Hi Dear Colleagues,
I need a behaviour script in scriptrunner, which will not allow to set the date greater then 1 year.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.