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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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).

That would be greatly appreciated! 

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.

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.

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

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
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

165 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you