Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Updating a cascading custom field value using workflow post functions (with Scriptrunner)

Graham.Wilson January 8, 2021

I'm using Adaptivist Scriptrunner. I need to clear both values within a cascading custom field when an issue transitions into IN PROGRESS. I can't figure out how to do this, and I can't see any examples on the Adaptivist site.

 

Can anyone give me a step by step guide? The simpler the better, as this is something I have very little experience of. Actual code where I can just substitute field names would be ideal! 

 

Edit: No "you can do it using this paid for app" answers please. 

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
January 8, 2021

I can paste those scripts, after verifying for myself they work (probably a year or so since I touched a cascading field so also want to refreshen up on that).

Graham.Wilson January 8, 2021

That would be greatly appreciated! 

Radek Dostál
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.
January 8, 2021

Alright I think this should be it:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig

OptionsManager optionsManager = ComponentAccessor.getComponent(OptionsManager)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField cascadingCF = customFieldManager.getCustomFieldObjectsByName("Activity").find {
it.getCustomFieldType() instanceof CascadingSelectCFType
}
if (!cascadingCF) {log.error("Cannot find cascadingCF field! Exiting."); return}

MutableIssue mutableIssue = (MutableIssue) issue

// to get parent value: currentValues.get(null)
// to get child value: currentValues.get("1")
Map<Object, LazyLoadedOption> currentValues = issue.getCustomFieldValue(cascadingCF) as Map<Object, LazyLoadedOption>

//Need to decide here which block you want to use in your use case
if (currentValues == null) { //is empty, set values
FieldConfig fieldConfig = cascadingCF.getRelevantConfig(mutableIssue)
Option parentOptionToSet = optionsManager.getOptions(fieldConfig).getOptionForValue("Parent 1", null) //(String optionName, long parentOptionId), so this is for parent
Option childOptionToSet = optionsManager.getOptions(fieldConfig).getOptionForValue("Child 1.1", parentOptionToSet.getOptionId()) //and this for child

Map<Object, Option> optionsToSet = new HashMap<Object, Option>() {
{
put(null, parentOptionToSet)
put("1", childOptionToSet)
}
}

mutableIssue.setCustomFieldValue(cascadingCF, optionsToSet)
}
else { //list is not empty, clear it
mutableIssue.setCustomFieldValue(cascadingCF, null)
}

Let me know in case this gives you any trouble or if you have any questions.

Radek Dostál
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.
January 8, 2021

Note that in workflow post-functions, this should be before "issue updated, added to changelog, yadayada" and re-index. So ideally somewhere near to top, either before or after "Status changed to destination step" one.

Graham.Wilson January 8, 2021

Thanks - I'll try that and let you know.

Graham.Wilson January 8, 2021

Ok that worked perfectly. Thanks so much for your time and effort, hugely appreciated.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events