Groovy script - Trouble setting value for custom multi select field

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
Henning Tietgens Community Champion Sep 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

That did it. Thanks Henning.

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

Depends on the jira version...

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.

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.

0 vote

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

168 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you