Missed Team ’24? Catch up on announcements here.

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

ScriptRunner, Issues with required custom field based on another field

Chuck Bishop November 7, 2016

Hi,

I have a requirement to make a custom field (Version Single Select) required based on the value picked in another custom field (Single select list - Yes or No).  I've experimented with numerous script combinations in the ScriptRunner Behaviors functionality, but I can never get the groovy script syntax correct to recognize the "Yes" selected value from the first custom field. 

From many of the documentation and suggestions I've found, I think this should be possible, but I'm missing something.  In my help text display, both values always show up as "Null"; it never recognizes the value selected.

Custom Fields:

"Regression Issue"  =  Single Select List w/ Yes or No

"Regressed from Version" = Version Single Select

 

Here's my current groovy script:

 

import com.onresolve.jira.groovy.user.FieldBehaviours

import com.onresolve.jira.groovy.user.FormField

 

def regissue = getFieldByName("Regression Issue")

def regver = getFieldByName("Regressed from Version")

 

String vregissue = (String) regissue.getFormValue()

String vregver = (String) regver.getFormValue()

 

//to see values near fields uncomment sentences

regissue.setHelpText("regissue: " + vregissue)

regver.setHelpText("regver: " + vregver)

 

regissue.setRequired(true)

 

if (vregissue == "Yes") {           

       regver.setRequired(true)

} else {

       regver.setRequired(false)

}

 

ScriptRunner.png

Any suggestions?

Thanks!

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

7 votes
Answer accepted
adammarkham
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.
November 8, 2016

I think you were quite close actually. The following code should allow you to do what you require:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def regressionIssueField = getFieldById(getFieldChanged())
def regressedVersionField = getFieldByName("Regressed from Version")

String regressionIssueValue = regressionIssueField.getValue()

if (regressionIssueValue == "Yes") {
    regressedVersionField.setRequired(true)
} else {
    regressedVersionField.setRequired(false)
}

You should add the "Regression Issue" field to the behaviour and attach this script by clicking on "Add serverside script"

The first example in the documentation here is very similar to what you need and is just an alternative way of doing the same as the example above.

As a side field.getFormValue() will get you the original value that was submitted in the form, which may be an option id or the actual string value. It best just to use field.getValue() in most cases.

Chuck Bishop November 8, 2016

Thanks!  I can't believe I was so close and totally missed the need to assign the script to the field!  Duh!!!  I think that was the root of my problem. 

I do have one more question.  I noticed that even with the Behaviour Fields option set to Writable, the JIRA in-line edit option for these fields is now disabled.   All 3 fields are disabled.  Is this expected? 

edit-option.png

adammarkham
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.
November 8, 2016

Yes this is expected. By default the inline edit is disabled for fields with behaviours.

You can try to enable it by adding this line to the script:

regressedVersionField.setAllowInlineEdit(true)
Like siva likes this
Chuck Bishop November 10, 2016

Thanks for the suggestion.  It didn't enable editing, however it would be nice to have, but not a huge deal though.  Thanks for your help!!

Rahul k May 17, 2019

@Adam Markham [Adaptavist]

Will this script work for checkboxes, i am struggling for the past couple of days to get this done. 

def AField = getFieldById(getFieldChanged())
def BField = getFieldByName("Resolution Required")

String TheProlem = AField.getValue()

if (TheProblem == "Resolution Required") {
    BField.setRequired(true)
} else {
    BField.setRequired(false)
}
0 votes
Deleted user May 6, 2019

@adammarkham . I'm a newbie to "Behaviors" and struggling with your solution, Adam, although I have a feeling it's the right way to go. Please tell me where I went wrong.

First, I went to Admin/Addons/Behaviors and applied the suggested script, updated (I hope) to reflect that I want my "Export Control - Country of Final Destination" field to be REQUIRED if the "DeliveryRequired?COPY" field that I'm testing has a value of "Yes". 

I have JSU and I've checked the "Use Validator Plugin" checkbox, although I'm not certain what that does. (Do I need to ALSO apply a JSU validator as part of the transition where I want this field to be required?). Won't that make the field required regardless of the value of another field? Showing my ignorance here.

Anyway, right now, nothing is happening when I transition the issues under test that use this workflow copy and the desired Behavior of the field at this transition. 

(I updated this comment to remove a lot of unnecessary images and descriptions. As it turns out, I figured out that I had to simply change the focus of the script from the "DeliveryRequired?COPY" field to the "Export Control - Country of Final Destination" field, as I described below).

 

Deleted user May 6, 2019

I think that I've figured it out. I was placing the behavior on the wrong field. When I applied the script to the correct field, it seems to work.

...


def cmdocIssueField = getFieldById(getFieldChanged())

def cmdocVersionField = getFieldByName("Export Control - Country of Final Destination")
String regressionIssueValue = cmdocIssueField.getValue()


if (regressionIssueValue == "Yes: EAR99 NLR to International or Foreign National") {cmdocVersionField.setRequired(true)}

else {cmdocVersionField.setRequired(false)}

...

Like Scott Howard likes this
Deleted user May 6, 2019

The only problem now is that, while it works when I'm creating or editing an issue, if I change the value after the issue was created to "Yes: EAR99 NLR to International or Foreign National", it will allow the issue to be transitioned without requiring the "Export Control - Country of Final Destination" field. I'm using a workflow screen with those fields for the transition. Maybe I have to use the default "Open, Edit & View" screen where this works? I thought Behaviors weren't impacted by what screen the custom field appears on? I'll keep plugging away at this. It's probably not as hard as I'm making it.

:)

TAGS
AUG Leaders

Atlassian Community Events