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!!
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:-
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:-
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:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.