Groovy script - Trouble setting value for custom multi select field

Anthony Pomtree September 16, 2013

I've been trying to create a script that will update a JIRA custom field (named CustomerJob) upon issue creation. I don't get any errors in the script, but it does not seem to be updating the field properly.

Below is the code I am trying to use. I apologize if it is a little messy as I have tried numerous things to no avail.

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
def cfCustomerJob = customFieldManager.getCustomFieldObjectByName("CustomerJob");
def cfCustomerJobValue = issue.getCustomFieldValue(cfCustomerJob);
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

if (cfCustomerJobValue[0].toString().equals("ABC"))
{
OptionsManager optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class);
Option option = optionsManager.getOptions(cfCustomerJob.getRelevantConfig(issue)).getOptionForValue("XYZ", null);
List<Option> newCustomerJob = new ArrayList<Option>();
newCustomerJob.add(option);
cfCustomerJob.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfCustomerJob), newCustomerJob), changeHolder);
}

Thanks in advance for the help.

3 answers

1 accepted

2 votes
Answer accepted
Henning Tietgens
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.
September 16, 2013

Try this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
// import com.atlassian.jira.event.type.EventDispatchOption
// import com.atlassian.jira.user.ApplicationUsers

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfCustomerJob = customFieldManager.getCustomFieldObjectByName("CustomerJob")
log.error "cfCustomerJob: $cfCustomerJob"
def cfCustomerJobValue = issue.getCustomFieldValue(cfCustomerJob)
log.error "cfCustomerJobValue: $cfCustomerJobValue"

if (cfCustomerJobValue[0].toString().equals("ABC"))
{
    log.error "cfCustomerJobValue[0].toString().equals(\"ABC\") is true"
    OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
    IssueManager issueManager = ComponentAccessor.getIssueManager()
    Option option = optionsManager.getOptions(cfCustomerJob.getRelevantConfig(issue)).getOptionForValue("XYZ", null);
    log.error "Found option: $option"
    issue.setCustomFieldValue(cfCustomerJob,[option])
    // issueManager.updateIssue(ApplicationUsers.toDirectoryUser(ComponentAccessor.jiraAuthenticationContext.getUser()), issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

My guess is you're using the script as a postfunction. Maybe you have to uncomment the commented lines and use this as the last postfunction in the list of postfunction to work. This is for JIRA 6. I added some log errors you should see in the log file. If everything is fine you can change the methods to log.debug. Or you could start with log.debug and set the log level accordingly.

Henning

Anthony Pomtree September 17, 2013

That did it. Thanks Henning.

Sumit Kumar
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.
October 17, 2013

Hi Henning,

have you checked this for post function ,.... I think it is not working

ComponentAccessor.jiraAuthenticationContext.getUser() returns ofBiz while only ApplicationUser are allowed in ApplicationUsers.toDirectoryUser()

Thanks,

Sumit

JamieA
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.
October 18, 2013

Depends on the jira version...

0 votes
JamieA
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.
September 16, 2013

Add some logging... log.warn(option) etc. Otherwise you can't know what's not working.

0 votes
Nancy Blackwell
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.
September 16, 2013

What type of field is "CustomerJob"? Are you sure you are hitting the if? If it is a single select box, then I don't believe you can use array coding, the value is a list of Options.

Anthony Pomtree September 16, 2013

It is a multi select field. With that said, it will only ever have one selected value. It should have been configured as a single select field during implementation, but it was mistakingly not. It is my understanding the only way to change its type is to either update the DB directly, or do some sort of mass export/import (into a new single select field). But for now, it will likely remain as is.

Suggest an answer

Log in or Sign up to answer