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

Script Runner Behaviour: Validation on FieldChange - how to get originalIssue value?

Manuel July 27, 2023

Hi,

I want to create a field check in a script runner behaviour.

When the field “duedate” is changed i want to validate:

1)  if the new value is not in future > show error in GUI

2) if the new date is in future > okay

3) If the field is not changed, nothing should happen.

 

As far as i know i cannot use “issue” in behaviours, instead of it i must use "underlyingIssue".

To compare if a field was changed i use usualy the simple line:

issue.duedate != originalIssue.duedate 

But i was not able to find something similar like "oiginalIsse" or "originalValue" for underlyingIssue. Is there anything similar i can use in behaviour?

Best Regards

Manuel

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave 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.
July 28, 2023

If you need to specifically query the value that's currently stored in the data base for the issue while in a behaviour, use the underlyingIssue like you would use the "issue" object present in many other scriptrunner areas.

For due date, you can use the field directly

def originalDueDate = underlyingIssue.duedate

For custom fields, 

def originalCustomFieldValue= underlyingIssue.getCustomFieldValue(customField)

 Then, you can compare those against the "formfield" value. Be sure to account for possible differences in data types and formats.

Manuel July 31, 2023

Many thanks - thats the solution. 

0 votes
Answer accepted
Radek Dostál
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.
July 28, 2023

There isn't any such a thing as "originalIssue". What you get is the issue when the script is running.

The part that you think you need to do the comparison with 'if the field is not changed' - you don't need to, your script simply shouldn't be running at all in that situation.

 

See this example below:

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

@BaseScript FieldBehaviours fieldBehaviours

/*
Needs to be attached to 'Due Date' system field
*/

final String ERROR_MESSAGE = "Due Date cannot be set in past/present."

FormField dueDateField = getFieldById(getFieldChanged())
Date dueDateValue = (Date) dueDateField?.getValue()

if (dueDateValue != null && dueDateValue.before(new Date())) {
dueDateField?.setValid(false)
dueDateField?.setError(ERROR_MESSAGE)
}
else {
dueDateField?.setValid(true)
dueDateField?.clearError()
}

 

It is attached to the Due Date field. It will only run when the field's value is being changed by the user (or when the field is being cleared, in which case it resets the field back to valid). I think this actually covers all the 3 requirements.

Manuel July 31, 2023

Sure - FormField :)

Many thanks for the detailed answer. I was so concentrated in getting something similiar to originialissue...

TAGS
AUG Leaders

Atlassian Community Events