It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to - Changing value of custom field dropdown makes 2 other fields required (Groovy ScriptRunner)

I am using Jira Software 8.4.3 and ScriptRunner 5.6.7.1-jira8

Using Behaviors, I have a custom field which is a dropdown list and when the value "Yes" is selected, I expect to see 2 other custom fields to change to "required". When I switch back to a value of "No" I expect to see the other 2 fields NOT required and hidden or read only. I have found forum posts online to do all of this, but I am not seeing any real time changes when the dropdown values are changing, which was behavior seen in tutorial videos.

If I update the JIRA issue and re-open that issue, only then do I see that the other two fields have become required.

I believe this question is also asking about seeing this "dynamic" changing of required/not required on the fly:

https://community.atlassian.com/t5/Adaptavist-questions/Updating-Fields-dynamically-in-Jira-Service-Desk-Portal-with/qaq-p/832735

At the bottom of this the person asking the question has put the following code:

if ((getFieldById(getFieldChanged()).value as Priority)?.name == "High") { ... }

I think this is what I am looking for but as I have a custom field I am not sure how to modify this to make it work for my needs.

What I have coded so far is the following:

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;

def costImpField = getCustomFieldManager().getCustomFieldObject("customfield_10400");
def costImpValue = (String)underlyingIssue?.getCustomFieldValue(costImpField);

def sbirsProgImpField = getFieldById("customfield_10619")
def govtImpField = getFieldById("customfield_10620")

if (costImpValue.equals("Yes")) {
sbirsProgImpField.setRequired(true)
//sbirsProgImpField.setHidden(false)
//sbirsProgImpField.setReadOnly(false)
govtImpField.setRequired(true)
//govtImpField.setReadOnly(false)
//govtImpField.setHidden(false)
} else {
sbirsProgImpField.setRequired(false)
//sbirsProgImpField.setHidden(true)
//sbirsProgImpField.setReadOnly(true)
govtImpField.setRequired(false)
//govtImpField.setHidden(true)
//govtImpField.setReadOnly(true)

}

1 answer

1 accepted

0 votes
Answer accepted

Hi @Justin G 

The behaviour should be attached to the field to which you want to list for changes.

getFieldChanged() always returns the field ID of the field the behaviour fires for. You can find more information with example in this Link.

def formField = getFieldById(getFieldChanged())
def sbirsProgImpField = getFieldById("customfield_10619")
def govtImpField = getFieldById("customfield_10620")

def selectedOption = formField.getValue() as String
if (selectedOption == "Yes") {
do something
}
else {
do something
}

Hope this helps!

Thank you, that worked, I was accessing the fields incorrectly and this was much simpler than I had coded it to be.  I had the behavior on the correct field but accessing the values was wrong. thanks much

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you