Forums

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

Groovy: How to update single option custom field?

Alexander Yordanov
Contributor
February 12, 2019

Hello,

 

I'm writing a code that depending on the Request Type from the portal it should update a custom field called "IT Change Type". The custom field has the options: "Normal", "Urgent" and "Retro". I'm getting the option for the custom field, and the custom field but it still doesn't update it.

 

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;


}

Sample log from the code:

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

But then the field in the issue is not updated even tho there are no visible errors.

Any idea?

Alex

1 answer

1 accepted

0 votes
Answer accepted
evzensx February 12, 2019

You just set value, but not save it. Try something like that in your switch\case construction:

def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validationResult = issueService.validateUpdate(loggedInUser, issue.id, issueInputParameters)

if (change) {
log.warn "Value changed from ${change.oldstring} to ${change.newstring}"
//commentManager.create(issue,user,comment,true)
if (validationResult.isValid()) {
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
else {
log.warn event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field}
}
Alexander Yordanov
Contributor
February 13, 2019

Hey, thanks that helped a lot! After the switch/case I added

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

And it worked!

Thanks,

Alex 

Like evzensx likes this

Suggest an answer

Log in or Sign up to answer