Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Depends on the jira version...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Add some logging... log.warn(option) etc. Otherwise you can't know what's not working.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.