Error updating select list in a post function "ScriptRunner workflow action"

I am trying to update a select list to a specific value and it doesn't seem to like "setFormValue".   I verified the right value of the value I need is 16112.  What am I doing wrong?

I tried following the example here https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/setting-default-fields.html#_setting_defaults_for_selects_etc

 

#################

Inline Script

#################

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf = customFieldManager.getCustomFieldObjectByName("Package Type");

Map cfVal = issue.getCustomFieldValue(cf) as Map
String value = cfVal.get(null)

if (value == 'Web (WEB)')
{
    CustomField cf2 = customFieldManager.getCustomFieldObjectByName("DB Pkg");   
    def optionsManager = ComponentAccessor.getOptionsManager()
    def config = cf2.getRelevantConfig(issue)
    def options = optionsManager.getOptions(config)
    def optionToSelect = options.find { it.value == "DBA should be consulted" }
    cf2.setFormValue(optionToSelect.optionId)
}

#################

Error

#################

Script function failed on issue: TEST-123, actionId: 313, file: <inline script>

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.fields.ImmutableCustomField.setFormValue() is applicable for argument types: (java.lang.Long) values: [16112]

1 answer

Hi Micelle, 

So I think you confused setting a value in a post function and setting a value using a behaviour. Those two concepts are different. So in a sentence the behaviour makes changes in the client sied (front end) in constrast chenges in a post function are "back end" changes and saved / indexed.

So for the post function the script that sets a value to a select list (dusting a transition) will be something like 

import com.atlassian.jira.component.ComponentAccessor

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("SelectListA")
def fieldConfig = cf?.getRelevantConfig(issue)
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "AAA"}
issue.setCustomFieldValue(cf, optionToSet)

 Hope that helps,

Thanos

Thanks Thanos!   That was very helpful.  Because I set the field based on the value of another field (cascading (first option)) I had to add a little bit more.  Here is the entire "Custom script post-function (inline script)" script...

##################################

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf1 = customFieldManager.getCustomFieldObjectByName("Package Type");

Map cfVal = issue.getCustomFieldValue(cf1) as Map
String value = cfVal.get(null)

if (value == 'Web (WEB)')
{
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("DB Pkg")
def fieldConfig = cf?.getRelevantConfig(issue)
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "DBA should be consulted"}
issue.setCustomFieldValue(cf, optionToSet)    
}

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 Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

176 views 0 4
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