Forums

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

Set custom field value using groovy script

Mamikon Papikyan
Contributor
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
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
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
Product Admin
TAGS
AUG Leaders

Atlassian Community Events