Forums

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

Update Cascading Select options via Scriptrunner REST endpoint

Sasha Solig
Contributor
March 23, 2021

Hi,

I have a working REST endpoint script which allows me to reload a very large number of single select options. This is required to periodically reload options which come from an external SAP system and contains many thousands of options. The script is executed from within an Excel spreadsheet and performs these functions for a single select custom field:

1. Disable all existing options

2. For each row in the spreadsheet:

    a. Check if the existing option already exists in the select list.

    b. If found, enable the option

    c. If not found, add the option

3. Sort options

Now... I have a similar requirement for a cascading select. My problem is that while I can iterate through the parent level options, I can't see a way to iterate the child options. The code below only finds the parent option, the child options size is always 0 (the cascading select DOES have child options) and no child option is ever returned.

private Option getChildOption(List<Option> rootOptions, String value) {
  for (Option parent : rootOptions) {
    log.info("Parent=" + parent.getValue())
    if (parent.getValue().equals(value)) {
      List<Option> childOptions = parent.getChildOptions();
      log.info("Child options size=" + childOptions.size())
      for (Option child : childOptions) {
        log.info("Child=" + child.getValue())
        return child;
      }
    }
  }
  return null;
}

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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 29, 2021

I can never wrap my head around for loops ... I'm so much more confortable with groovy methods.

Here is how I would write a function that takes a list of rootOptions and returns all the child values based on the supplied parent value:

import com.atlassian.jira.issue.customfields.option.Options

def getChildOptions(Options rootOptions, String parentValue){
def parentOption = rootOptions.find{it.value == parentValue}
if(parentOption){
parentOption.childOptions.collect{it.value}
}
}

Note the import. The OptionaManager.getOptions(fieldConfig) method returns this Options collection.

If you want to return the full child option objects and not just the string value, omit the colect{it.value} part.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.3
TAGS
AUG Leaders

Atlassian Community Events