Jira Cascading field update from script runner

Muhammad Bajwa March 23, 2018

Hey,

I am writing a listener script for Jira that updates the value of a cascading field from a text field.

I am able to pull the value from the text field but I am not sure how to set them to the cascading fields. I found a guide online but it does not seem to work https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/setting-default-fields.html#_setting_cascading_select_values

I am getting an error on getFieldByName(fieldName), the error says this method does not exist.

I have not been able to find any solution for this online.

2 answers

1 accepted

5 votes
Answer accepted
Kyle Moseley
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.
March 25, 2018

The link you have applies to Behaviours (part of SR) but not other parts of SR, like Scripted Listeners.

 

You'll want to use groovy like this:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def changeHolder = new DefaultIssueChangeHolder()
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey('SPB-5')
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10600)
def fieldConfig = customField.getRelevantConfig(issue)
def parentOption = ComponentAccessor.getOptionsManager().getOptions(fieldConfig)?.find { it.toString() == 'North America' }
def childOption = ComponentAccessor.getOptionsManager().findByParentId(parentOption.optionId)?.find { it.toString() == 'USA' }
def newValue = [:]
newValue.put(null, parentOption)
newValue.put("1", childOption)
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValue),changeHolder)

 

See OptionsManger API documentation for more details on how to get options. 

Muhammad Bajwa March 26, 2018

This worked, Thanks!!!

0 votes
dominik.drexl March 5, 2019

Hallo togther,

I tried quite the same and my cascading field isn't updated with this code.
I update this field within the Standard-Function of scriptrunner "Clones an issue, and links" in the field additional issue actions. You see in the code, that I also set the summary, which works.

For the code above I don't receive any errors in the log after processing.
The log-output which I added to control if everythings fine also delivered the values, I expexcted:

2019-02-26 12:39:29,487 ERROR [utils.ConditionUtils]: parentOption:SAP
2019-02-26 12:39:29,489 ERROR [utils.ConditionUtils]: childOption:Berechtigung
2019-02-26 12:39:29,489 ERROR [utils.ConditionUtils]: customFieldName:Vorgangskategorie
2019-02-26 12:39:29,489 ERROR [utils.ConditionUtils]: customFieldId:customfield_12803
2019-02-26 12:39:29,491 ERROR [utils.ConditionUtils]: customFieldKey:Vorgangskategorie
2019-02-26 12:39:29,492 ERROR [utils.ConditionUtils]: fieldConfig:com.atlassian.jira.issue.fields.config.FieldConfigImpl@b7a27a59
2019-02-26 12:39:29,492 ERROR [utils.ConditionUtils]: 0:SAP
2019-02-26 12:39:29,492 ERROR [utils.ConditionUtils]: 1:Berechtigung

The only thing is, that the update in the cloned issue isn't updates.

This is my Code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

issue.summary = 'SAP Berechtigungen für ' + issue.summary
def changeHolder = new DefaultIssueChangeHolder()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12803)
//def customField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Vorgangskategorie'}

def fieldConfig = customField.getRelevantConfig(issue)
def parentOption = ComponentAccessor.getOptionsManager().getOptions(fieldConfig)?.find { it.toString() == 'SAP' }
def childOption = ComponentAccessor.getOptionsManager().findByParentId(parentOption.optionId)?.find { it.toString() == 'Berechtigung' }

log.error("parentOption:" + parentOption)
log.error("childOption:" + childOption)
log.error("customFieldName:" + customField.getName())
log.error("customFieldId:" + customField.getId())
log.error("customFieldKey:" + customField.getNameKey())
log.error("fieldConfig:" + fieldConfig)

//def newValue = [:]
def newValue = new HashMap();
newValue.put(null, parentOption);
newValue.put("1", childOption);
log.error("0:" + newValue.values().getAt(0));
log.error("1:" + newValue.values().getAt(1));

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValue),changeHolder)

Could you please help me further.

Thanks in advance.

Dominik

Suggest an answer

Log in or Sign up to answer