It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Set Customfield Value on Create

I have many instances of setting various customfield values in our workflows and I'm not having issues with them. This particular one seems to be giving me trouble:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

Issue issue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField Customer = customFieldManager.getCustomFieldObject("customfield_12400") // 12400 - Customer
CustomField Category = customFieldManager.getCustomFieldObject("customfield_12401") // 12401 - Category

//String issueCategory = Category
//String issueCustomer = Customer

String issueCategory = issue.getCustomFieldValue(Category)
String issueCustomer = issue.getCustomFieldValue(Customer)

if(Customer == "@No Customer"){
String internalCategory = "Request - EDCO"
Category.updateValue(null, issue, new ModifiedValue(issueCategory, internalCategory), new DefaultIssueChangeHolder());
String externalCategory = "Request - Customer"
Category.updateValue(null, issue, new ModifiedValue(issueCategory, externalCategory), new DefaultIssueChangeHolder());

I'm sure it's something simple but I can't for the life of me figuring out what is causing my issues. Errors read as follows:

2019-12-10 19:13:17,454 ERROR [workflow.AbstractScriptWorkflowFunction]: *************************************************************************************
2019-12-10 19:13:17,454 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: EDCO-77223, actionId: 1, file: null
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option
	at com.atlassian.jira.issue.customfields.impl.SelectCFType.getDbValueFromObject(
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(
	at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(
	at com.atlassian.jira.issue.fields.OrderableField$ Source)

Is something different wen updating values from the create transition? This same method seems to be working fine further down the line in our workflows.

1 answer

1 accepted

0 votes
Answer accepted

Hi @Steven Mustari 

You can only update select custom fields with an com.atlassian.jira.issue.customfields.option.Option, not a String.

To get an Option instance, please follow below steps.

- import OptionsManager 

import com.atlassian.jira.issue.customfields.manager.OptionsManager

- Find appropriate option instance using code as below

def option = ComponentAccessor.optionsManager.getOptions(Category)?.find {    it.toString().equals("Request - EDCO")

 - use this option instance in your updateValue method

ps: I did not test the code

I hope it helps.


Thank you for the response!

It wasn't exactly what I was looking for, however, it certainly pointed me in the direction I needed to go to find what I needed!


Glad to hear that

Suggest an answer

Log in or Sign up to answer

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you