Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner: how to display custom field based on value in Assets object field?

Ashley Honeycutt
Contributor
May 22, 2024

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

1 answer

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2024 edited

Hi @Ashley Honeycutt

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:-

behaviour_config.png

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:-

test1.png

2. If the option India is selected from the Country Asset field, the field Number 1 is hidden as shown in the screenshot below:-

test2.png

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:-

test3.png

test4.png

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

 

Ashley Honeycutt
Contributor
May 23, 2024

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

}

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, team '25, conference, certifications, bootcamps, training experience, anaheim ca,

Want to make the most of Team ‘25?

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
AUG Leaders

Upcoming Jira Events