Set Customfield Value on Create

Steven Mustari
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.
December 10, 2019

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());
}
else{
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(SelectCFType.java:72)
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
	at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
	at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
	at Script885.run(Script885.groovy:28)

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
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2019

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.

Tuncay

Steven Mustari
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.
December 17, 2019

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!

Regards,
Steven

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 17, 2019

Glad to hear that

Suggest an answer

Log in or Sign up to answer