Set custom field value using groovy script

Mamikon Papikyan May 29, 2020

Hi, I'm taking the value of "MyCustomField" field, using this script.

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectByName("MyCustomField")
def cFieldValue = issue.getCustomFieldValue(cField)

 so and I have other custom field e.g. ("TestCustomField"). How can I set value of cFieldValue to "TestCustomField" using groovy script?

1 answer

1 vote
Peter-Dave Sheehan
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 1, 2020

The easiest way to accomplish this will depend on where you want to execute that script.

For example, if a workflow Post Function, you just need to

def cField2 = customFieldManager.getCustomFieldObjectByName('TestCustomField')
issue.setCustomFieldValue(cField2, cFieldValue)

This will work as long as the 2 fields are of the same type and your Post Function is before the built-in function that stores the issue.

If you want to do this elsewhere, like in a listener, then you need to take care of storing and indexing the updated issue.

def cField2 = customFieldManager.getCustomFieldObjectByName('TestCustomField')
issue.setCustomFieldValue(cField2, cFieldValue)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
issueManager.updateIssue(currentUser, issue,EventDispatchOption.DO_NOT_DISPATCH, false)
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
indexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing);


Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events