Forums

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

Adaptavist ScriptRunner Behaviour - Hide field based on value of other field

Deleted user August 10, 2020

I am trying to execute a simple behaviour using Adaptavist ScriptRunner.  On a workflow transition screen, I have 2 fields:

  • customfield_10907 is a select list (single) field
  • customfield_28501 is a radio button field

If the user selects "Training" in customfield_10907, then customfield_28501 should appear.

If the user selects any other value, then customfield_28501 should be hidden.

 

Here is my server-side script:

def CategoryField = getFieldById("customfield_10907")
def moreTrainingField = getFieldById("customfield_28501")

if (CategoryField.getValue() == "Training") {
moreTrainingField.setHidden(false)
} else {
moreTrainingField.setHidden(true)
}

The behaviour is not not working.  customfield_28501 appears on the transition screen regardless of the selection in customfield_10907

Any suggestions on how to fix this so it will work?

1 answer

1 accepted

1 vote
Answer accepted
Tamas Baglyas August 10, 2020

Hi 

I checked your code, modified a little bit, and it has to be work, I tested. If you set the proper project and issue type(s), and add the Category select field (10907) to the Fields, add the script like a Server-side script it has to be work.
If the fields appear together only in that transition screen you don't have to be do anything else. But is you use them together you should use an action, to avoid unexpected events.

/**
* Atlassian Community answer
* <a href="https://community.atlassian.com/t5/Jira-questions/Adaptavist-ScriptRunner-Behaviour-Hide-field-based-on-value-of/qaq-p/1453585" target="_blank">Adaptavist ScriptRunner Behaviour - Hide field based on value of other field</a>
* @author Tamás Baglyas - https://github.com/tbaglyas
* @version 1.0
* @since 2020-08-11
*/
if (getActionName().equals("Transition name where you want to use these behaviours.")) {
boolean hidden = !(getFieldById("customfield_10907")?.getValue()?.toString()?.equals("Training"));
getFieldById("customfield_28501").setHidden(hidden);
}

Cheers,
Tamás

Deleted user August 11, 2020

Hi @Tamas Baglyas .  Thanks for the reply.  It is very close but doing the opposite of what I need.   Currently, if the users selects Training, custom field 28501 is hidden.  I want custom field 28501 visible only if user selects Training.  Here is the script:

if (getActionName().equals("Validate Requirement")) {
boolean visible = getFieldById("customfield_10907")?.getValue()?.toString()?.equals("Training");
getFieldById("customfield_28501").setHidden(visible);
}

Tamas Baglyas August 11, 2020

Hi @[deleted] ,

 

Thanks, I modified my script based on your clarification. I think it is closer now.

 

Cheers,

Tamás

Deleted user August 11, 2020

@Tamas Baglyasthanks again.  It is working as expected!

Alex
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.
November 17, 2023

@Tamas Baglyas Hi!

I was looking for a solution and found your answer. Thank you! This works great!
I add this is code in BEHAVIOUR . It's worked !

Be sure to check the context. The code should be in the “field” tab, and not in the “Initialiser”"

boolean hidden = !(getFieldById("customfield_24511")?.getValue()?.toString()?.equals("Yes"))

getFieldById(
"customfield_24512").setHidden(hidden)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events