Forums

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

How to make 2 fields as cascaded with scripts

Rajesh Kumar June 23, 2023

We have 2 custom field single select, Want to convert those fields as cascaded with script or any other option except default cascaded fields option.

Does any one have idea on this?

Thanks for Support!!!

Thanks,

Rajesh

1 answer

0 votes
Deepak Jain
Community Champion
June 23, 2023

Hi @Rajesh Kumar ,

Welcome to Atlassian community !!

Unfortunately it is not possible Jira out of the box. But you can achieve it using Script runner Bahaviours:

https://marketplace.atlassian.com/apps/1228522/behaviours-scriptrunner-for-jira-cloud?tab=overview&hosting=cloud

Here is the sample script, considering two select list:

  • First Field - A, B C
  • Second Field - A1, A2, A3, B1, B2, B3, C1, C2

Replace correct field id customfield_XXX1, customfield_XXX2 in code:

 

import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.*
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def firstField = getFieldById("customfield_XXX1")
def firstFieldSelectedValues = firstField.value as List

def secondField = getFieldById("customfield_XXX2")
HashSet<String> selectedOptions=new HashSet();

for (option in firstFieldSelectedValues) {
def optionName = option.toString();

if(optionName.trim().equalsIgnoreCase("A")){
selectedOptions.addAll(new HashSet<String>(Arrays.asList("A1", "A2", "A3")));
}
if(optionName.trim().equalsIgnoreCase("B")){
selectedOptions.addAll(new HashSet<String>(Arrays.asList("B1", "B2", "B3")));
}
if(optionName.trim().equalsIgnoreCase("C")){
selectedOptions.addAll(new HashSet<String>(Arrays.asList("C1", "C2")));
}
}
if (firstFieldSelectedValues != [null] && selectedOptions.size() > 0) {
secondField.setFieldOptions(selectedOptions)
}

Hope this helps !!

Rajesh Kumar June 26, 2023

Hi Deepak,

Thanks for the reply

I have used your script and changed the field ids as per my requirement. 

Added behavior and selected first field for mapping tried to excecute the same.

But after clicking 'Add' it wont allowing me to move forward.

// Insert the Jira UI modifications API script for the behaviour.
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.*
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def firstField = getFieldById("customfield_10049")
def firstFieldSelectedValues = firstField.value as List

def secondField = getFieldById("customfield_10050")
HashSet<String> selectedOptions=new HashSet();

for (option in firstFieldSelectedValues){
    def optionName = option.toString();
    if(optionName.trim().equalsIgnoreCase("A")){
    selectedOptions.addAll(new HashSet<String>(Arrays.asList("A1", "A2", "A3")));
    }
    if(optionName.trim().equalsIgnoreCase("B")){
        selectedOptions.addAll(new HashSet<String>(Arrays.asList("B1", "B2", "B3")));
    }
    if((optionName.trim().equalsIgnoreCase("C")){
        selectedOptions.addAll(new HashSet<String>(Arrays.asList("C1", "C2")));
    }
    if(firstFieldSelectedValues != [null] && selectedOptions.size() > 0){
        secondField.setFieldOptions(selectedOptions)

    }

}
 
and it is showing some compile errors (red lines) as shown below

image.png

 

Kindly help us.

Thanks

Rajesh

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events