Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner behaviour: how do I use 'contains'?

Sabine Van Regenmortel
Contributor
July 22, 2022

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:

  • if field A contains value "Client", then show field B (this works with ==)
  • if field A contains value "Press", then show field C (this works with ==)
  • if field A contains values "Client" and "Press", then show field B and C (does not work with ==)
  • if field A contains values "Press" and "blablabla", then show field C (does not work with ==)

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:

 

[Static type checking] - Cannot find matching method java.lang.Object#contains(java.lang.String). Please check if the declared type is correct and if the method exists.
Possible solutions: toString(), toString(), notify()

3 answers

2 accepted

2 votes
Answer accepted
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
July 22, 2022

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()

?

Sabine Van Regenmortel
Contributor
July 22, 2022

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.

Florian Bonniec
Community Champion
July 22, 2022

Hi @Sabine Van Regenmortel , what is the type in JIRA of the field with id customfield_16044 ?

Sabine Van Regenmortel
Contributor
July 22, 2022

Hi @Florian Bonniec , it is a checkbox.

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
July 22, 2022

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?

Florian Bonniec
Community Champion
July 23, 2022

Ok @Sabine Van Regenmortel 

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

Like Craig Nodwell likes this
Sabine Van Regenmortel
Contributor
July 23, 2022

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"

Florian Bonniec
Community Champion
July 23, 2022

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

Like Sabine Van Regenmortel likes this
Sabine Van Regenmortel
Contributor
July 23, 2022


Hi @Florian Bonniecthanks for your solution.

Please also note that I set the sliders of field B and field C to Hidden.
Untitled.png
Both string and list work.  The script bullet turns green and the behaviour is already better:

  • if field A contains value "Client" and "blablabla", then field B is shown
  • if field A contains value "Press" and "blablabla", then field C is shown
  • if field A contains values "Client" and "Press" and "blablabla", then both field A and B are shown

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);
}
Florian Bonniec
Community Champion
July 23, 2022

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.

Like Sabine Van Regenmortel likes this
1 vote
Answer accepted
Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2022

Hi @Sabine Van Regenmortel,

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.

Ravi Kanth
Contributor
February 1, 2024

Hi @Andrea Pannitti 

 

Thank you so much it worked..

 

Thanks,

N.Ravikanth

2 votes
Florian Bonniec
Community Champion
July 22, 2022

Hi @Sabine Van Regenmortel 

 

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events