Groovy script: Update single select custom field.

Alexander Yordanov
Contributor
February 12, 2019

Hello,

 

I have a script that is looking into the Customer Request Type and depending on it, it updates a custom field called "IT Change Type" with the correct option. Options are "Norma", "Retro" and "Urgent".

Here is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

MutableIssue mutableIssue = (MutableIssue) issue;
log.info("issue: " + mutableIssue.toString());
def reqType = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('Customer Request Type');
def reqTypeVal = mutableIssue.getCustomFieldValue(reqType).getValue();
log.info("Issue type: " + reqTypeVal);

def changeTypeId = 14430;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomField changeType = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(changeTypeId);


def fieldConfig = changeType.getRelevantConfig(issue);
switch (reqTypeVal) {
case "itcm/e89992e0-cbf1-4208-ad24-e8ea100f2c33":
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Normal' };
log.info("field option: " + value.toString());
log.info("field name: " + changeType.toString());
mutableIssue.setCustomFieldValue(changeType, value);
break;

case "itcm/3b6cb133-0971-48c4-8b53-d5a49af828ca":
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Urgent' };
log.info("field option: " + value.toString());
log.info("field name: " + changeType.toString());
mutableIssue.setCustomFieldValue(changeType, value);
break;

case "itcm/74b2e0f8-33ef-4a14-a905-f1ef7460d6ae":
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Retro' };
log.info("field option: " + value.toString());
log.info("field name: " + changeType.toString());
mutableIssue.setCustomFieldValue(changeType, value);
break;


}

It seems like everything is working fine but the issue is not updated. Here is a sample log:

2019-02-12 06:22:48,441 [http-nio-8080-exec-4] | issue: ITCM-66
2019-02-12 06:22:48,443 [http-nio-8080-exec-4] | Issue type: itcm/74b2e0f8-33ef-4a14-a905-f1ef7460d6ae
2019-02-12 06:22:48,444 [http-nio-8080-exec-4] | field option: Retro
2019-02-12 06:22:48,444 [http-nio-8080-exec-4] | field name: IT Change Type

The script finds the option to the field correctly and the field itself but doesn't update the issue with the new value. There are no errors which is weird.

Any ideas?

Alex

1 answer

1 accepted

0 votes
Answer accepted
Alejandro Suárez García
Atlassian Partner
February 12, 2019

Hi @Alexander Yordanov, I assume that you are using a Scripted Postfunction to achieve this. In which postfunction place did you put this script? Is in the create transition or another one?

The code seems fine, but the way you did it, you have to ensure that the script is running in the first place, or at least before the "update" postfunction.

Regards! 

Alexander Yordanov
Contributor
February 13, 2019

Hey, I'm using the Riada's Insight plugin which has script console. I'm running it there and i can specify an issue key so it will be run against that issue. The script will be running in a Workflow Insight Post Function on the create transition but as a last function on it. Right now I'm testing the script in the console.

Alejandro Suárez García
Atlassian Partner
February 13, 2019

Hi @Alexander Yordanov that code you post won't do anything if you place it in the last place. This is because the methods you are using to update the fields, don't update the issue itself. 

Here is the code for the last place (untested, but should work):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
OptionsManager optionsManager = ComponentAccessor.getOptionsManager()

CustomField reqType = customFieldManager.getCustomFieldObject(10002L),
changeType = customFieldManager.getCustomFieldObject(14430L)
Option reqTypeVal = issue.getCustomFieldValue(reqType) as Option
FieldConfig fieldConfig = changeType.getRelevantConfig(issue)
Option value

switch (reqTypeVal.value) {
case "itcm/e89992e0-cbf1-4208-ad24-e8ea100f2c33": value = optionsManager.getOptions(fieldConfig)?.find { it.getValue() == 'Normal' }; break
case "itcm/3b6cb133-0971-48c4-8b53-d5a49af828ca": value = optionsManager.getOptions(fieldConfig)?.find { it.getValue() == 'Urgent' }; break
case "itcm/74b2e0f8-33ef-4a14-a905-f1ef7460d6ae": value = optionsManager.getOptions(fieldConfig)?.find { it.getValue() == 'Retro' }
}

if(value)changeType.updateValue(null, issue,new ModifiedValue(issue.getCustomFieldValue(changeType), value),new DefaultIssueChangeHolder())
Alexander Yordanov
Contributor
February 13, 2019

Yeah, that's the case. I actually forgot to update the issue.

I solved it with this after the switch/case:

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
ComponentAccessor.getIssueManager().updateIssue(loggedInUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

Thanks! 

Alejandro Suárez García
Atlassian Partner
February 13, 2019

Thats right too, but you are adding unnecesary code to your script in this case, because you can update the value of the customfield itself, updating the issue at the same time.

I forgot to tell that is convenient in this cases to add a reindex line, if not, you will have an unconsistent data until you reindex the issue:

ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
Like Alexander Yordanov likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events