Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,797
Community Members
 
Community Events
165
Community Groups

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

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

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

 

 

 

 

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
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,350 views 36 44
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you