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);
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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 '!').
def labelToFieldId =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.
['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));
}
Hope this is helpful.
Regards,
J
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@ParthibanThere is also an option to accept both of our solutions. But it is up to you to decide:) Have a good day!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.