Showing field based on multiselect component label

Hi Team,

I want to show custom field based on Component selection. I have written code in Script Runner Behaviour , my code working perfectly when users select one component label. But its not behaving correctly when users select two or more component labels.


If users select two or more component labels , I have to show two or more custom fields. 

Note : I have created 40 custom fields and 40 component labels for my client. When ever users select component I need to show/display respective custom fields.

My code is working when users select one component label.

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.acme.CreateSubtask");

def dashboardCost = getFieldById("customfield_12735");
def devCost = getFieldById("customfield_12736");
def sccdCost = getFieldById("customfield_12737");
def jiraCost = getFieldById("customfield_12738");

def components =getFieldById("components");
def comp= components.getValue()*.getName();

if (comp.toString().contains('Dashboard')){
else if(comp.toString().contains('dev')){
else if (comp.toString().contains('SCCD')){
else if (comp.toString().contains('JIRA')){

2 answers

1 accepted


What exactly is happening when you select more than one component label? Knowing this will make it easier for us to diagnose the problem and suggest a fix.

One initial suggestion I would make, however, is not to call toString() on comp. The comp variable is a list of strings so the contains() method will work fine with it, without needing to turn the whole thing into a string first.

Hi Jake,


Thanks for your suggestion , I removed toString() from my coding still its working fine when I select single component label.

If I select more than one component label , it always pick the first component label .

Because I have set the condition like wise, can you tell me how to modify my code.




Hi Parthiban,

As Alexey has indicated, the reason that only the first label is getting picked is because your if-blocks are chained together with 'else'. As soon as one of the if-conditions succeeds, the code corresponding to that condition will run and all of the following if-blocks will be skipped over.

Alexey's code seems entirely correct to me (although as I said before, you can get rid of the toString() if you want) but I will suggest some slight alternatives for the sake of variety:

  • Because setHidden() takes a boolean, which is what your if-conditions are testing in the first place, you can in principle cut out the if-statements altogether and do something like this:
  • dashboardCost.setHidden(!comp.contains('Dashboard'))

    Essentially we find out whether the components list includes 'Dashboard' and then set the hidden attribute of the dashboardCost field to the opposite of that (hence the use of '!').

  • You say in your original question that you have 40 each of custom fields and component labels, so I assume that the code you posted is just a small snippet of the whole thing. Given that you're doing essentially the same thing for each field/label pairing, there's going to be a lot of repetition in your code which will make it harder to modify later. You could eliminate a lot of this repetition by gathering all of the pairings together into a Map and looping over it:
  • def labelToFieldId =
    ['Dashboard': "customfield_12735",
    'dev': "
    'SCCD': "customfield_12737",
    'JIRA': "customfield_12738",

    def components = getFieldById("components");
    def comp = components.getValue()*.getName() ?: [];

    labelToFieldId.each { label, fieldId ->
    def field = getFieldById(fieldId);
    Please note that I haven't tested the above code but it's just to give you the general idea of how you could do it.

Hope this is helpful.



Hi Jake,

Thanks a lot.!! It worked exactly as per my need. I working on this for past one month on this requirement. Thanks for your great support.




Hi Parthiban,

I'm glad to hear that you've been able to get it working! If you think that Alexey's and my answers have been helpful to you, then please consider accepting one of them, so that in future other users with a similar problem to you will be able to see that a solution is available.

Many thanks,


Alexey Matveev Community Champion Jan 05, 2018

@ParthibanThere is also an option to accept both of our solutions. But it is up to you to decide:) Have a good day!

@Alexey Matveev Ah, good point - sometimes I forget how Community differs from, say, Stack Overflow. Obviously the choice is yours @Parthiban but I'd say Alexey's answer is just as important to the solution.

1 vote
Alexey Matveev Community Champion Jan 03, 2018


I guess it does not work because you use else if. Only first condition fires. You need to rewrite it to

if (comp.toString().contains('Dashboard')){
} else {


} else {


if (comp.toString().contains('SCCD')){
} else {


if (comp.toString().contains('JIRA')){
} else {



Hi Alexey/Jake,


Thanks for writing me back. 

Yup,  I know my code was wrong . May I know how to achieve my requirement. 

If I select more than one component label , how to get the list of Component and size of component selected.



Alexey Matveev Community Champion Jan 03, 2018

Your code is right. The problem is about if statements. I gave the the right code in my previous post. If you want to get the size of the list of the selected components you should do it like this

List<String> comp= components.getValue()*.getName();
def compSize = comp.size()

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

237 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you