Bulk update a portion of a read-only text field with Scriptrunner

Bryan Guffey May 31, 2022

Hi all -

I have a read-only text field that stores an hyperlink to a ServiceNow Problem URL. When we do refreshes of our non-prod environments, the link still points to the production instance of ServiceNow. I'd like to update the custom field to point to the appropriate non-prod instance of ServiceNow, but I can't quite figure out how to update only part of the custom field instead of replacing the entire thing.

For example, the field may contain something like:


<a href="https://testinstance.service-now.com/nav_to.do?uri=problem.do?sys_id=87c9fe41db0cc8d4f2d7f3931d961906">PRB00142356</a>

and the only part I want to update is the URL so it reads

<a href="https://sandboxinstance.service-now.com/nav_to.do?uri=problem.do?sys_id=87c9fe41db0cc8d4f2d7f3931d961906">PRB00142356</a>

So far I've only found this script on the Adaptavist Library to bulk update custom fields. However, it seems to replace the entire string, instead of just one piece. Anyone have any suggestions? 

Thank you!!

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2022

Hi @Bryan Guffey

For your requirement, you could use the ScriptRunner console with something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def project = projectManager.getProjectByCurrentKey('MOCK') // change to your project key
def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

def field1 = customFieldManager.getCustomFieldObjectsByName('Field 1').first() // change to your field name

issues.sort().each { issue ->
def oldValue = issue.getCustomFieldValue(field1).toString()
def newValue = oldValue.replace('testinstance.service-now.com', 'sandboxinstance.service-now.com')
field1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field1), newValue), new DefaultIssueChangeHolder())
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the configuration:-

console_config.png

Also, I include a few test screens for your reference:-

1. Below are some screenshots of when the issues are first created with the old URL:-

image1.pngimage2.pngimage3.png

If you notice the screenshots above, all the issues MOCK-1, MOCK-2 and MOCK-3 are using the old URL when they are first created. And the fields are set to read-only.

2. Once the script is run on the ScriptRunner console, the values in Field 1 are updated as expected, as shown in the screenshots below:-

update1.pngupdated2.pngupdated3.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

 

 

 

 

Milos Pejkovic June 13, 2022

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

This is really useful, thank you!

Is there a way to replace the text in Description and Comments also?

BR,
Milos

Suggest an answer

Log in or Sign up to answer