Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Script replace certain String in a custom field in all issues

Andreas Lorz
Contributor
October 28, 2024

Hello everyone,

We have a custom field where a URL is entered. Parts of this URL will change in the future. Is there a Scriptrunner solution to go over each issue, looking into a custom field. If the field value contains a particular string, replace that part of the value with a new string value.

 

An Example.

Issues that have in custom field: CF, value: "https://abc.d.com/browse/ABC-1" should be change by the script to: "https://xyz.d.net/browse/ABC-1"

Issues that haven't a Substring "abc.d.com" in the custom field should be skippted.

Thanks in Advance.

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2024

Hi, @Andreas Lorz 

 

Yes, it's possible. It's not very complicated case. 
You need to select issues by JQL, which contain value in required custom field.

 

Then, for each of issue replace part of string with required text.
You can use something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String jqlQuery = "project = YOUR_PROJECT_KEY AND \"URL Custom Field Name\" IS NOT EMPTY" // Update JQL accordingly
def issues = Issues.search(jqlQuery)

CustomField customField = customFieldManager.getCustomFieldObjectByName('URL') // Update to your field name
String textToReplace = "part_of_string_to_replace"
String replacementText = "new_text"

issues.each { issueObject ->
MutableIssue issue = Issues.getByKey(issueObject.key) as MutableIssue
String customFieldValue = issue.getCustomFieldValue(customField)
if (customFieldValue && customFieldValue.contains(textToReplace)) {
String updatedValue = customFieldValue.replace(textToReplace, replacementText)

issue.setCustomFieldValue(customField, updatedValue)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.info("Updated issue ${issue.key} with new value: $updatedValue")
}
}

log.info("Completed updating issues.")
Andreas Lorz
Contributor
October 31, 2024

Thanks, the script works like a charm and is exactly what I needed.

0 votes
Answer accepted
Jim Knepley - ReleaseTEAM
Atlassian Partner
October 28, 2024 edited

Hi @Andreas Lorz 

ScriptRunner can easily work on every issue in a query result: https://library.adaptavist.com/entity/update-cf-values-all-issues

Matching one (or more) URL strings in a custom field will require some care, since regular expressions are a language unto themselves. This post on Stack Overflow has some examples and references:

https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url

Once you tell it what a URL looks like, ScriptRunner can use matches for flow control:

https://docs.adaptavist.com/sr4js/7.9.0/features/workflows/conditions/regular-expression-condition

...and then do the replacement with Groovy's "replaceAll()" method:

https://www.tutorialspoint.com/groovy/groovy_replaceall.htm

Andreas Lorz
Contributor
October 31, 2024

Thanks for the explanation and the resources with the background knowledge. This is exactly what I was missing.

DEPLOYMENT TYPE
SERVER
VERSION
9.12.2
TAGS
AUG Leaders

Atlassian Community Events