Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

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

1 vote
Answer 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,


Thanks Jake. 

@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 _Appfire_ 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.


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.



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

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

179 views 1 6
Read article

Atlassian Community Events