Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Behavior to expose field based on change in Due Date.

Pete P
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.
October 18, 2022

I would like to expose and require a "Reason" custom text field when the "Due Date" field is changed from the original value.  Does anyone know the proper syntax? 

1 answer

0 votes
PD Sheehan
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 10, 2022

Doing a bit of digging for older unanswered questions and found this one...

Behaviours can't "expose" a field ... you have to add the Reason field to your edit (or transition) screens first.

Then, you can use behaviours to hide the Reason field by default, but run a script when the Due Date is changed that will trigger the Reason field to be displayed and flagged as required

One trick though... you might want to cancel the changes if the user realizes this and want to just set back the original due date (rather than close the form and potentially lose other edits).

So the script has to detect when the field is changed, and when it's actually different than the stored value.

Something along these lines should work (add to the due date field server-side script in behaviors)

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
if(!underlyingIssue) return //nothing to do when creating (there is no stored issue yet)

def dueDateField = getFieldById(fieldChanged)
def reasonFld = getFieldByName('Reason')

def showReason = underlyingIssue.dueDate && underlyingIssue.dueDate != dueDateField.value
reasonFld.setHidden(!showReason).setRequired(showReason)
if(!showReason){
//clear out the reason field if we're hiding it so we don't actually submit a reason for change when we've changed out mind and not making the change
reasonFld.setFormValue(null)
}

The top 3 lines are not needed (but won't hurt anything) if you store the script "in-line". They are helpful however if you store your script as a file. This will help compilation in the script editor or external IDE.

Suggest an answer

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

Atlassian Community Events