Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to update select list (cascade) with post function Script Runner

Kristján Geir Mathiesen
Contributor
April 26, 2018

Hi

I´ve got this script where I am trying to update the custom field "Groups" with the parent value "S D" and child value "T S":

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def fieldName = "Groups"
def field = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObjectByName(fieldName)
def fieldConfig = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find {it.value == "S D"}
def childOption = parentOption?.childOptions?.find {it.value == "T S"}

field.setFormValue([parentOption.optionId, childOption.optionId])

 

But I keep getting this error:

2018-04-26 19:40:45,578 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ABC-5, actionId: 291, file: <inline script>
groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script696
	at Script696.run(Script696.groovy:1)

 Any ideas what I am doing wrong?

Thanks so much,

KGM 

 

3 answers

1 accepted

0 votes
Answer accepted
Kristján Geir Mathiesen
Contributor
April 27, 2018

Found out with a help of a friend. This works as a post function script:

import com.atlassian.jira.issue.CustomFieldManager
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.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue

Issue issueKey = issue
def issueManager = ComponentAccessor.getIssueManager()
def MutableIssue currentissue = issueManager.getIssueObject(issue.getKey())
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def textCf3 = customFieldManager.getCustomFieldObjects(currentissue).find {it.name == "The name of the custom field"}
def changeHolder = new DefaultIssueChangeHolder()
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();

Map<String,Object> newValues = new HashMap<String, Object>()
Option parentOptionObj = optionsManager.findByOptionId(new Long(10010 i.e. the ID number of the value you want to update to));
Option childOptionObj = optionsManager.findByOptionId(new Long(10011 i.e. the ID number of the value you want to update to));
newValues.put(null,parentOptionObj)
newValues.put("1",childOptionObj)
textCf3.updateValue(null, currentissue, new ModifiedValue(currentissue.getCustomFieldValue(textCf3), newValues),changeHolder)

0 votes
Sana Safai
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.
April 26, 2018

Can you post a screenshot?

Kristján Geir Mathiesen
Contributor
April 26, 2018

.

Sana Safai
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.
April 26, 2018

I will quote myself in my first reply: "Any other errors similar to that will need a similar type of import."

Your error has to do with the customFieldManager.

Try these import statements:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.customfields.option.*;

Kristján Geir Mathiesen
Contributor
April 26, 2018

I´ve added these and some more onto the import list (long list now) and am getting a new error:

 

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getFieldByName() is applicable for argument types: (java.lang.String) values: [Groups]
 at Script723.run(Script723.groovy:10)

 

Sorry, I´m not really a programmer (as is obvious), just trying to get through this "little" task :)

Thanks, KGM 

0 votes
Sana Safai
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.
April 26, 2018

Hi,

Try starting your script with

"import com.atlassian.jira.component.ComponentAccessor"

You need to import the ComponentAccessor class. Any other errors similar to that will need a similar type of import.

Kristján Geir Mathiesen
Contributor
April 26, 2018

Thank you @Sana Safai  I still get the same error though :/  Any other ideas?  Thanks, KGM

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events