Way to make one field required based on another field selection using scriptrunner behaviors

Joe Harmon April 25, 2023

I have a required field called "Release Type" and when it is set to "Hardware" I want to have the SKU(s) field required.  When it is set to anything else, I want the SKU(s) field optional and read only or if possible hidden.  Here is the script I have so far but it isn't working at all.  I also want the asterisk to show against skus as well if possible.

 

def releaseTypeField = getFieldById(getFieldChanged())

def releaseTypeStatus = releaseTypeField.getValue()

def skusField = getFieldById("SKU(s)")

if (releaseTypeStatus == "Hardware") {
    skusField.setRequired(true) //Sets the SKUs field to required.
    skusField.setHelpText("The SKUs field is required when it is a hardware project")
}
else {
    skusField.setRequired(false) //Sets the SKUs field back to optional
    skusField.setReadOnly(true)
    skusField.setHelpText("")
}

 

1 answer

1 accepted

1 vote
Answer accepted
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2023

Hi @Joe Harmon 

You use getFieldById but you provided cf name. Use getFieldByName or provide ID of the field.

def skusField = getFieldById("SKU(s)")

Also, use formField.setHelpText() to see the values in UI for testing.

Ex; releaseTypeField.setHelpText(releaseTypeStatus)

For further info:

Joe Harmon April 25, 2023

That's what it was.  I saw an example from someone and that is what they used and had the name on it.  I was confused by that as I figured it had to the be the customfieldID_xxxx.  Oh well, thanks for helping me with it.

Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2023

Glad to hear that, you are welcome!

Joe Harmon April 26, 2023

I tried to add a second field with the same context as the first and it only worked on the first.  You ever heard of that before?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events