Groovy to compare 2 date fields

Raghu Ram June 6, 2019

The requirement is as below:

We have 3 fields - Outage Start Date, Change Start Date, Change End Date (all are Date Time picker fields). When someone tries to EDIT an issue, one should not be able to select an Outage Start Date that falls outside the Change window (which means Outage Start Data >= Change Start Date and Outage Start Date <= Change End date).

Can someone please provide a sample script to help. 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2019

Hi @Raghu Ram , welcome to the community.

This is one script you can use with scriptrunner's behaviours : 

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

@BaseScript FieldBehaviours fieldBehaviours

int changeStartDate1Id = 11001
def changeStartDate = getFieldById("customfield_" + changeStartDateId)
def changeStartDateValue = changeStartDate.getValue()

int changeEndDateId = 11002
def changeEndDate = getFieldById("customfield_" + changeEndDateId)
def changeEndDateValue = changeEndDate.getValue()

int outageStartDateId = 11003
def outageStartDate = getFieldById("customfield_" + outageStartDateId)
def outageStartDateValue = outageStartDate.getValue()

if (outageStartDateValue != null && outageStartDateValue != ""){
if (changeStartDateValue != null && changeStartDateValue != "" && outageStartDateValue.compareTo(changeStartDateValue) < 0){
outageStartDate.setError("Outage start date can only be set after the change start date.")
}
else if (changeEndDateValue != null && changeEndDateValue != "" && outageStartDateValue.compareTo(changeEndDateValue) > 0){
outageStartDate.setError("Outage start date can only be set prior the change end date.")
}
}

Update the custom fields ids. You need to create a behaviour for each field.

Antoine

Raghu Ram June 7, 2019

Hi Antoine,

Thanks a lot for the detailed response.

I have created a behaviour called Outage Start Date, mapped it correctly with Project and Issue Type, changed the field ids in the script and saved it.

Now when I try to Create a change request, in the Create form, when I enter the Change Start Date, Change End date and then give a wrong Outage Start Date, it shows "Outage start date can only be set after the change start date." right away. And then it stays like that even if we select a correct Outage Start Date.

Also, the script that I am saving (in Behaviours) it shows 2 errors:

if (changeStartDateValue != null && changeStartDateValue != "" && outageStartDateValue.compareTo(changeStartDateValue) < 0)

else if (changeEndDateValue != null && changeEndDateValue != "" && outageStartDateValue.compareTo(changeEndDateValue) > 0)

The error is Please check if the declared type is correct and if the method exists.

Do you have any idea what might have gone wrong? Thanks in advance.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2019

Hi @Raghu Ram ,

If the error appears then then you should not worry about the errors (groovy will evaluate the object types correctly at execution time).

How did you configure your behaviours ? You should have the same behaviour repeated 3 times : one for each field. A behaviour executes each time you changed the field value, that is why you should re-evaluate the condition each time one of these 3 fields is updated.

Hope that is clear enough.

Raghu Ram June 7, 2019

That worked. You are amazing, thanks a lot.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2019

You're welcome ! If you are satisfied with the answer, feel free to accept it so it can help other users. :)

Raghu Ram June 10, 2019

I am really satisfied with the answer. Just one final clarification if it isn't too much trouble:

When I try to create an issue for this project, on the Create form, I select the Change start date, Change End date, and then select an Outage start date (which is outside the change window) and it shows the error as per the script. However if I then rectify the timing and adjust it inside the change window, the error message still doesn't disappear. It doesn't allow me to Create the issue either. This is bit of a problem.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 11, 2019

Hi @Raghu Ram , that is an issue indeed. :)

Please update the if block :

if (outageStartDateValue != null && outageStartDateValue != ""){
if (changeStartDateValue != null && changeStartDateValue != "" && outageStartDateValue.compareTo(changeStartDateValue) < 0){
outageStartDate.setError("Outage start date can only be set after the change start date.")
return
}
else if (changeEndDateValue != null && changeEndDateValue != "" && outageStartDateValue.compareTo(changeEndDateValue) > 0){
outageStartDate.setError("Outage start date can only be set prior the change end date.")
return
}
}
outageStartDate.clearError()
Raghu Ram June 12, 2019

Worked like a charm! Thanks a lot.

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer