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

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

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");
log.setLevel(Level.DEBUG);

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')){
dashboardCost.setHidden(false);
}
else if(comp.toString().contains('dev')){
devCost.setHidden(false);
}
else if (comp.toString().contains('SCCD')){
sccdCost.setHidden(false);
}
else if (comp.toString().contains('JIRA')){
jiraCost.setHidden(false);
}

2 answers

1 accepted

1 vote
Answer accepted

Hi,

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.

 

Thanks,

Parthiban

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': "
    customfield_12736",
    'SCCD': "customfield_12737",
    'JIRA': "customfield_12738",
    <etc...>
    ];

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

    labelToFieldId.each { label, fieldId ->
    def field = getFieldById(fieldId);
    field?.setHidden(!comp.contains(label));
    }
    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.

Regards,

J

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.

 

Thanks,

Parthiban

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,

J

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.

Hello,

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')){
dashboardCost.setHidden(false);
} else {

dashboardCost.setHidden(true);

}
if(comp.toString().contains('dev')){
devCost.setHidden(false);
} else {

devCost.setHidden(true);

}
if (comp.toString().contains('SCCD')){
sccdCost.setHidden(false);
} else {

sccdCost.setHidden(true);

}
if (comp.toString().contains('JIRA')){
jiraCost.setHidden(false);
} else {


jiraCost.setHidden(true);

}

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.

Thanks,

Parthiban

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

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across Jira Cloud instance.   📢 Get y...

161 views 7 8
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