Forums

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

Scriptrunner behaviour - make field mandatory based on another field selection

Bojan Virag
Contributor
May 31, 2023

I set up a behaviour to make a field mandatory when another field is set to a certain value. The behaviour works, but I need to switch issuetype back and forth for the required condition to update.

This is my server side script:

"

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FormField

def field1 = getFieldByName("Field 1")
def field2 = getFieldByName("Field 2")


if (field1.getValue() == "Value 1") {
field2.setRequired(true)
} else {
field2.setRequired(false)
}

"

I tried to set it to run on Workflow Action:Create (1) and without, but does not change anything. Not sure what I'm missing?

2 answers

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
May 31, 2023

You need to attach the script to the field you want to react on. I assume you currently have this on an Initializer - which will only run once (that is why it works once you switch the forms w/ issue type, it reloads the initializer).

Add the field you're reacting on, then attach the script to it. It should then fire that script every time that you modify the field.

(If you want the "text format" from the select list, then field#getFormValue() should do that I believe)

Bojan Virag
Contributor
June 2, 2023

Thank you! I was actually attaching the script to the field I was setting to mandatory instead of the field I was reacting on.

Works great now!

0 votes
Tansu Akdeniz
Community Champion
May 31, 2023

Hi @Bojan Virag 

If script doesn't work, can you try this?

String field1Val = (String) field1.getValue()
field1.setHelpText("Value: " +field1Val)

It will show you the value in create screen (for debug purpose). 

Bojan Virag
Contributor
May 31, 2023

@Tansu Akdeniz thank you, this is very helpful! 

default value is Value: Form field ID: customfield_12300, value: -1

When I change field1 nothing happens, but then if I change the issue type, value changes to Value: Value: Form field ID: customfield_12300, value: 17449

Also this is a single select drop down text field, I was expecting text to be returned.

Tansu Akdeniz
Community Champion
May 31, 2023

You are welcome! So, comparing value instead of text should solve.

Suggest an answer

Log in or Sign up to answer