I'm trying to create a Scriptrunner Behavior that displays a custom field based on the value in an Assets Object custom field.
This script works if both fields are regular custom fields. It also works as-is to hide the text field, but doesn't display the field when the expected option is selected.
def cf1 = getFieldByName("[ASSET OBJECT FIELD NAME]")
def cf2 = getFieldByName("[CUSTOM MULTI-LINE TEXT FIELD NAME")
def cf1Value = cf1.value as String
if (cf1Value == "[ASSET OBJECT FIELD VALUE]") {
cf2.setHidden(false)
cf2.setRequired(true)
} else {
cf2.setHidden(true)
cf2.setRequired(false)
}
Any help is greatly appreciated!
Thank you
Your Behaviour code is not working with the Assets because you cannot directly get the value from the Asset field.
By default, when you select an Asset from an Asset List, it will not return the value of the Asset displayed but the key of the Asset.
To overcome this, you must use ScriptRunner's HAPI feature for Assets.
Below is a sample working code for your reference:-
import com.adaptavist.hapi.jira.assets.Assets
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def country = getFieldById(fieldChanged)
def countryValue = country.value.toString()
def number1 = getFieldByName('Number 1')
number1.hidden = false
number1.required = false
if (countryValue) {
if (Assets.getByKey(countryValue).name == 'India') {
number1.hidden = true
} else if (Assets.getByKey(countryValue).name in ['Malaysia', 'Singapore']) {
number1.required = true
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Behaviour configuration:-
If you observe in the screenshot above, I have created a Server-Side Behaviour for the Asset field called Country.
Hence, I have initialised the field using the fieldChanged value, i.e.:-
def country = getFieldById(fieldChanged)
Whenever you use a Server-Side Behaiour, it is best to initalised the field the Server-Side Behaviour is configured for using the fieldChaged option. This ensures that the Behaviour will only trigger if there is a change in the value made to that field.
Below are a couple of test screenshots for your reference:
1. First, when no option is selected from the Country Asset field, both the fields Number 1 and Number 2 are visible as shown in the screenshot below:-
2. If the option India is selected from the Country Asset field, the field Number 1 is hidden as shown in the screenshot below:-
3. However, if either the option Malaysia or Singapore is selected from the Country Asset, the field Number 1 is made visible and required as shown in the screenshots below:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
@Ram Kumar Aravindakshan _Adaptavist_
Thank you so much! That's exactly what I needed to get me there. I updated my script to the following based on your feedback, which gave me the results I needed:
def cf1 = getFieldByName("ASSET OBJECT FIELD NAME")
def cf2 = getFieldByName("CUSTOM MULTI-LINE TEXT FIELD NAME")
def cf1Value = cf1.value as String
if (Assets.getByKey(cf1Value).name == "ASSET OBJECT FIELD VALUE") {
cf2.setHidden(false)
cf2.setRequired(true)
} else {
cf2.setHidden(true)
cf2.setRequired(false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.