I want to hide a field, depending on the value of another field. This works for exact matches using ==
But what about a multiple select list? I want to express:
So I want to use contains and tried something like this:
import com.atlassian.jira.component.ComponentAccessor
def fieldA= getFieldById("customfield_16044")
def fieldB = getFieldById("customfield_18340")
def fieldC = getFieldById("customfield_18341")
def Clientstring = "Client"
def Pressstring = "Press"
log.debug("dropdown value" + fieldA.getValue())
if (fieldA.getValue().contains(Clientstring)){
fieldB.setHidden(false);
fieldC.setHidden(true);
} else
if (fieldA.getValue().contains(Pressstring)){
fieldB.setHidden(true);
fieldC.setHidden(false);
}
But line (fieldA.getValue().contains(Clientstring)) results in error:
Hi @Sabine Van Regenmortel , what is the type of
fieldA.getValue()
?
Can you check it using:
log.debug "Type of fieldA value:" + fieldA.getValue().getClass()
?
Hi @Martin Bayer [MoroSystems, s.r.o.] ,
I'm afraid I don't know how to read the log. I just copied the script from the internet and tried it out.
I added your line but now what?
Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Sabine Van Regenmortel it is very useful to have the possibility to read the log files. Are you the administrator of your environment? Can you access your server's file system?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There is one thing with checkbox field type when using Behaviour is that it do not returned the same type of object depending on how many option you have selected in your field. If you select 1 value only it will return a String, if you select multiple value it will returned an Array of String.
So you have to managed both case, can you try something like
def fieldA= getFieldById("customfield_16044")
def fieldB = getFieldById("customfield_18340")
def fieldC = getFieldById("customfield_18341")
def Clientstring = "Client"
def Pressstring = "Press"
List fieldAValues = []
if(fieldA.getValue() instanceof String){
fieldAValues.add(fieldA.getValue() as String)
}else{
fieldAValues = fieldA.getValue() as List
}
if (fieldAValues.contains(Clientstring)){
fieldB.setHidden(false);
fieldC.setHidden(true);
} else if (fieldAValues.contains(Pressstring)){
fieldB.setHidden(true);
fieldC.setHidden(false);
}
You should add this script as Server Side script for the field customfield_16044 in your behavior, not at initializer.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Martin Bayer [MoroSystems, s.r.o.] , yes, I have access.
I can't find any occurrences of the string Type of fieldA value in the atlassian-jira.log file. Or in which log file do I have to search?
When I execute an sql
select customfieldtypekey from customfield where id=16044
this returns
"com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Sabine Van Regenmortel , if your not familiar with log and log level you can use
log.error instead of log.debug. Based on the level of log you log file may or may not log into it. Using log.error should be write into the log file. Once you figureout and everything will work find you can remove it or replace it back with log.debug to avoid writing too much unnecessary logs into the log file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Florian Bonniecthanks for your solution.
Please also note that I set the sliders of field B and field C to Hidden.
Both string and list work. The script bullet turns green and the behaviour is already better:
But:
when I uncheck values Client en Press, fields A an B don't disappear.
I also had to leave out the line setHidden(true) because I want both fields to appear when both values are checked.
Do I have to define all combinations unchecked/checked with 10 fields A,B, C etc.?
if (fieldAValues.contains(Clientstring)){
fieldB.setHidden(false);
} else
if (fieldAValues.contains(Pressstring)){
fieldC.setHidden(false);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Because you have to manage visibility by script at some point at would recommand you to not use the sliders for fields, so it may avoid conflict or be easier to understand.
You can create a initializer on the top of your behaviour configuration that set all fields as hidden. The initializer is run when the screen is loading.
So something like
def fieldB = getFieldById("customfield_18340")
def fieldC = getFieldById("customfield_18341")
fieldB.setHidden(true)
fieldC.setHidden(true)
Regarding the unchecked/checked it will depends if a field is visible based on a single option or multiple, let say fieldA is displayed if option A or B is selected.
If 1 option display one field and let say if in field A option Client is select then display field B, if Press is selected display field C and option X is selected display field D you should be able to do something like
def fieldA= getFieldById("customfield_16044")
def fieldB = getFieldById("customfield_18340")
def fieldC = getFieldById("customfield_18341")
def Clientstring = "Client"
def Pressstring = "Press"
List fieldAValues = []
if(fieldA.getValue() instanceof String){
fieldAValues.add(fieldA.getValue() as String)
}else{
fieldAValues = fieldA.getValue() as List
}
if (fieldAValues.contains(Clientstring)){
fieldB.setHidden(false);
}else{
fieldB.setHidden(true);
}
if (fieldAValues.contains(Pressstring)){
fieldC.setHidden(false);
}else{
fieldC.setHidden(true);
}
But if CLient is selected you want to display field B but not field C you should have to manage all compinaisons I' m afraid.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
the method getValue() return a generic object and the checkbox value is a list; so to use the method contains(), you have to cast the value to List or directly to string with toString(). So, you could modify your code in two ways:
if ((fieldA.getValue() as List).contains(Clientstring))
Or
if (fieldA.getValue().toString().contains(Clientstring))
I also recommend that you enter the code both in Initialiser and on the fields used.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Like @Martin Bayer [MoroSystems, s.r.o.] suggest it' s a good starting point to know what type of object you are dealing with.
The error means that it did not find any method to apply with String parameter to Object.
If fieldA.getValue() return a String you may have to use [fieldA.getValue()].contains(Clientstring) but I don' t see the purpose of using contains instead ==.
It's relevant if you are dealing with field that store multiple values but you have to make sure that it's an array of String that is returned and not an array of Object such as Option in case of a select list.
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.