Forums

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

Behaviour in ScriptRunners

Gor Greyan
Contributor
May 12, 2025

Hi Dear Colleagues,

I need a behaviour script in scriptrunner, which will not allow to set the date greater then 1 year.

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 14, 2025

Hi @Gor Greyan

Could you provide a little bit more details, i.e. what Date are you referring to?

Is it a custom Date Field?

When do you want the validation to take place? Is it when the Issue is being created, or when the Issue is being transitioned, or when you are editing the Issue?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Rma

Gor Greyan
Contributor
May 14, 2025

Hi Ram,
Yes, my field is Custom Date Field.
I want to put that behaviour in the create screen, when choosing the date from that field, it should not allow to select date greater than one year.

Gor Greyan
Contributor
May 15, 2025

Hi @Ram Kumar Aravindakshan _Adaptavist_
Did you manage to check my answer?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 16, 2025

Hi @Gor Greyan

Sorry, I've been really busy with projects.

For your requirement, you must create a Server-Side Behaviour for the Date field you intend to validate.

Below is a sample working code for your reference:-

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

@BaseScript FieldBehaviours behaviours
def dateField = getFieldById(fieldChanged)
dateField.clearError()

if (dateField.value) {
def dateFieldValue = dateField.value as Date
def currentDate = new Date(System.currentTimeMillis())
if ( daysBetween(currentDate, dateFieldValue) > 365 ) {
dateField.setError('The Date Value Must Not Exceed 1 Year')
}
}

static def daysBetween(def startDate, def endDate) {
use(TimeCategory) {
def duration = endDate - startDate
return duration.days
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

Please note, in the screenshot above, the editor is showing a few red lines. These are not coding errors, but are static type checks. In other words, the code works and is able to return the expected result.

I hope this helps to answer your question. :)

Thank you and Kind regards,
Ram

Gor Greyan
Contributor
May 18, 2025

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
Thanks for the provided example, I set it and mapped it with my field, but still it allows me to open a ticket, while setting it to a greater date than a year.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 19, 2025

Hi @Gor Greyan

Could you please share the exact code you are using and screenshots of how the result appears in your environment?

Thank you and Kind regards,
Ram

Gor Greyan
Contributor
May 19, 2025

Screenshot 2025-05-19 221719.png

Gor Greyan
Contributor
May 19, 2025

Dear @Ram Kumar Aravindakshan _Adaptavist_ ,
The result is that it allows me to create the ticket, despite the fact that I choose the date, for example, 2030.
I mapped it with my field which is To field.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 20, 2025

Hi @Gor Greyan

The code is not working because there is an error in the Annotation used.

I noticed that the code that I had pasted in my previous comment was displaying the annotation incorrectly. I have corrected it.

Please modify the annotation in your code from:-

@BaseScriptFieldBehaviours behaviours

to:-

@BaseScript FieldBehaviours behaviours

and rerun the test.

Thank you and Kind regards,
Ram

Gor Greyan
Contributor
May 20, 2025

Dear @Ram Kumar Aravindakshan _Adaptavist_ ,
Thanks a lot, it works.

0 votes
Gor Greyan
Contributor
May 13, 2025

Any updates dear colleagues?

TAGS
AUG Leaders

Atlassian Community Events