How to show and hide custom field based on Component USING Behaviours scriptrunner

How can i show or hide some custom filed based on component . 9

4 answers

Hello Tasher,

This has already been answered by another adaptavist member here, if you need further assistance setting the behaviour, or you want another example more relevant to your case, let us know.

Cheers!

Dyelamos

How can i get the component value ??

Hello Tasher

As shown by the accepted answer in the previous link, you could do:

FormField fieldFF = getFieldByName("YOUR CUSTOM FIELD NAME HERE");
String fieldValue = fieldFF.getValue();

I would also recomend that you take a look at the behaviours documentation, which is filled with examples that might help you.

Good luck and if I can help you anymore let us know.

Dyelamos

Unable tout resolve class FormField 

I have this error i need to get thé name of component

may I please take a look at your full code? 

instead of using FormField, use def:

have your last 2 declarations in line 6 and 7 to be:

def fieldFF = ...
def fieldValue = ...

Now i don't have error on the code but it don't work 

A colleage has said that you could use the IDs instead of the names, might work better.

You can do that like this:

import static com.atlassian.jira.issue.IssueFieldConstants.*
def ff = getFieldById(COMPONENTS)

How to have the id of the component ??

copy and paste the code that I just sent you, the ID comes in the import that you see on the first line of the code, that import defines the variable COMPONENTS which will contain the ID of the component

It's the some without error but it don't work

At line 4:

ff.getvalue

Will be a Collection of ProjectComponent , not a String, since . That could be why it isn't working.

What can i do!?

Hello Tasher.

Next time, please copy and paste your code instead of taking a picture of your screen, it will be better for us both :)

Here is the code, it should work:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours
def ff = getFieldById(COMPONENTS)
def values = ff.getValue()*.getName()
def component = getFieldbyId("customfield_15001")
def nameV = getFieldbyId("customfield_15004")
def quality = getFieldbyId("customfield_15007")
def company = getFieldbyId("customfield_15006")

nameV.setHidden(true)
company.setHidden(true)
quality.setHidden(true)

if(values.toString().contains("USB")){
    nameV.setHidden(false)
    company.setHidden(false)
    quality.setHidden(false)
}

If it doesn't let us know

Cheers

 

Error on Line 7 : spead operator can only be used on collection type1496831962321-1542449449.jpg

Tasher,

Please stop sending pictures, it makes it very hard to replicate your code because I have to type it, copy and paste it. 

That gives you an error because the editor cannot tell that ff.getValue() will return a collection. Just update the code ignoring that error. It should work.

Cheers

 

It was funny "Please stop sending pictures," and this guy didn't get it. Thanks for the code. It worked! 

Hi Jay. Which code?
Cheers!
DY

Hi @Daniel Yelamos [Adaptavist] 

 I tested the script but it doens't function well.It hides the fields on the first part but when I add in the Component/s field the value that must trigger the fields appearance, it doesn't function at all.

 Here's my script also used in case you want to verify:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours
def ff = getFieldById("Component/s")
def values = ff.getValue()*.getName()
def pdv = getFieldById("customfield_10313")
def cp = getFieldById("customfield_10314")
def siret = getFieldById("customfield_10305")
def opera = getFieldById("customfield_10321")
def nbcode = getFieldById("customfield_10315")
def mont = getFieldById('customfield_10316')
def local = getFieldById("customfield_10317")
def monb = getFieldById("customfield_10318")
def formation = getFieldById("customfield_10320")
def doc = getFieldById("customfield_10319")

pdv.setHidden(true)
cp.setHidden(true)
siret.setHidden(true)
opera.setHidden(true)
nbcode.setHidden(true)
mont.setHidden(true)
local.setHidden(true)
monb.setHidden(true)
formation.setHidden(true)
doc.setHidden(true)

 

if(values == "Fraude"){
pdv.setHidden(false)
pdv.setRequired(true)
cp.setHidden(false)
cp.setRequired(true)
siret.setHidden(false)
siret.setRequired(true)
opera.setHidden(false)
opera.setRequired(true)
nbcode.setHidden(false)
nbcode.setRequired(true)
mont.setHidden(false)
mont.setRequired(true)
local.setHidden(false)
local.setRequired(true)
monb.setHidden(false)
monb.setRequired(true)
formation.setHidden(false)
formation.setRequired(true)
doc.setHidden(false)
doc.setRequired(true)
}

 

I have the same error "spead operator can only be used on collection type" but it's nothing like you said.

Hi @Daniel Yelamos [Adaptavist] 

 I tested the script but I have some issues on the second part of it. It works well when it had to hide fields but when the value of the component is given and has the expected value, it doesnt show the expected initially hidden fields. I used the behaviours of scriptrunner, I mapped the script for a specific project also and for a specific issue type, and like I said on the first part of the script, the fields are well hidden (bcause found by their IDs) but once the value of Component/s field is the one which is supposed to show the hidden fields, it doesn't function. Here's my script in case you want to check it (thanks in advance for your support):

import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*

def ff = getFieldById("COMPONENTS")
def values = ff.getValue()
def pdv = getFieldById("customfield_10313")
def cp = getFieldById("customfield_10314")
def siret = getFieldById("customfield_10305")
def opera = getFieldById("customfield_10321")
def nbcode = getFieldById("customfield_10315")
def mont = getFieldById('customfield_10316')
def local = getFieldById("customfield_10317")
def monb = getFieldById("customfield_10318")
def formation = getFieldById("customfield_10320")
def doc = getFieldById("customfield_10319")

pdv.setHidden(true)
cp.setHidden(true)
siret.setHidden(true)
opera.setHidden(true)
nbcode.setHidden(true)
mont.setHidden(true)
local.setHidden(true)
monb.setHidden(true)
formation.setHidden(true)
doc.setHidden(true)

 

if(values == "Fraude"){
pdv.setHidden(false)
pdv.setRequired(true)
cp.setHidden(false)
cp.setRequired(true)
siret.setHidden(false)
siret.setRequired(true)
opera.setHidden(false)
opera.setRequired(true)
nbcode.setHidden(false)
nbcode.setRequired(true)
mont.setHidden(false)
mont.setRequired(true)
local.setHidden(false)
local.setRequired(true)
monb.setHidden(false)
monb.setRequired(true)
formation.setHidden(false)
formation.setRequired(true)
doc.setHidden(false)
doc.setRequired(true)
}

 

By the way, I didn't provide a workflow to catch on since it's at the creation of the ticket this must function as expected and I also didn't activate validator because those fields aren't required but only for my specific case.

Thanks a lot for the support you'll provide.

Hi!

The best way you can actually do this is by debugging the script yourself.

For example, if you have an if condition like this:

if(myfield.ishidden() && myotherfield.value == "HELLO"){
myfield.show()
}

You can debug this using your log by putting statements like this:

log.debug myfield.ishidden()
log.debug myotherfield.value
log.debug myotherfield.value == "HELLO"
if(myfield.ishidden() && myotherfield.value == "HELLO"){
myfield.show()
}

That would output something like this in your log

true

hello

false

and you would know that hello != "HELLO" and you would know what to do about it.

Do you understand what I mean?

I can't help you in any other way because your behaviour makes use of custom fields, you would have to include the specific configuration of your fields and I would have to manually reproduce it. I'm confident this isn't a product bug, but a problem with the code and if you want I can help you debug it here.

Let's look at your if conditions and the values that are going in first and we'll take it from there. Is that okay?

Cheers!

DY

Hi @Daniel Yelamos [Adaptavist] 

 Yes I would like your help on this topic. 

 For the if condition, initially the component/s field is empty so the fields are hidden according to my code. But when I will add the value "Fraude" in component/s field, it must display all of the hidden fields. That's all .And those fields are some text fields and some radio button fields with no default value (they are not prefilled at all).

 So when they must be displayed, they must be required at the same time according to my code even if in workflow validator they are not marked as required and I didn't activate the workflow validator for this simple operation.

 Feel free to ask me other questions. 

Btw my fields are in the default field configuration and added to the screen of the issuetype i want to apply the script on.

Hi Gilles.

I'm not sure if you understand my comment, I asked for you to place the debugging statements and check your logs for results on the values that the debug statements display. Do you understand how to do this?

Cheers!

DY

Hi @Daniel Yelamos [Adaptavist]

 I don't got it sorry. Do I add this debug into my code or elsewhere?

Thanks

Yes Gilles, that's exactly what I mean.

Read my explanation again about how to debug an if statement. 

For example, for the end condition in your code you would do something like this:

log.debug "DEBUGING VALUES FROM BEHAVIOUR:"
log.debug values
if(values == "Fraude"){
pdv.setHidden(false)
pdv.setRequired(true)
cp.setHidden(false)
cp.setRequired(true)
siret.setHidden(false)
siret.setRequired(true)
opera.setHidden(false)
opera.setRequired(true)
nbcode.setHidden(false)
nbcode.setRequired(true)
mont.setHidden(false)
mont.setRequired(true)
local.setHidden(false)
local.setRequired(true)
monb.setHidden(false)
monb.setRequired(true)
formation.setHidden(false)
formation.setRequired(true)
doc.setHidden(false)
doc.setRequired(true)
}

 This will make a debug statement in your logs when you trigger the behaviour and you will be able to know what's in the variable "values" at time of execution. Does this make sense?

Cheers!

DY

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,686 views 3 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you