Compare two date fields and validate transition

Bojan Virag June 14, 2022

I have two custom date fields: Start Date and End Date.

I want to check if End Date is set before Start Date in which case I want to stop ticket creation and let the users know that End Date cannot be set before Start Date.

I do have scriptrunner, but I didn't find a straightforward solution.

Thank you!

2 answers

1 accepted

1 vote
Answer accepted
Andrea Pannitti
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 19, 2022

Hi @Bojan Virag,

the problem depends on the fact that you have used the script (which was for a Custom script condition) in the creation phase.
Thus, a condition cannot be used but a validator must be used. In this case, we can use a Simple scripted validator with the following code:

def startDate = cfValues['Start Date'] as Date
def endDate = cfValues['End Date'] as Date

return (startDate != null && endDate!= null && startDate <= endDate)
Bojan Virag July 19, 2022

Works perfectly, thank you!

jun lee January 25, 2023

I want to compare custom date field and today

 

Below script was succeeded.

-----------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*
import utils.*

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def workpercent = customFieldManager.getCustomFieldObject("customfield_13500")

CustomField finishDateField = customFieldManager.getCustomFieldObject("customfield_10230"); //finish date
long finishDate = (issue.getCustomFieldValue(finishDateField) as Date).getTime(); //finish date value

def today = (new Date() as Date).getTime() //current datetime


if(finishDate > today) {
issue.setCustomFieldValue(workpercent, 100 as double)
} else {
return true
}

------------

 

When I use grater script, it doesn't work. 

if(finishDate > today) --> if(finishDate >= today)

 

So I tested equal condition like below, but failed. 

if(finishDate.equals(today))

if(finishDate.isequal(today))

if(finishDate.compareTo(today) == 0)

 

How can I fix?

0 votes
Andrea Pannitti
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.
June 14, 2022

Hi @Bojan Virag

you could use a custom script condition with the following code:

import com.atlassian.jira.component.ComponentAccessor

def cfStartDate = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Start Date").first()
def cfEndDate = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("End Date").first()
def startDate = issue.getCustomFieldValue(cfStartDate) as Date
def endDate = issue.getCustomFieldValue(cfEndDate) as Date

passesCondition = (startDate != null && endDate!= null && startDate <= endDate)
Bojan Virag June 14, 2022

Thank you for the complete solution @Andrea Pannitti ! I will give it a try later today and post the result.

Andrea Pannitti
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.
June 20, 2022

Hi @Bojan Virag

if my solution is fine for you, please, could you accept it?

 

Thank you

Bojan Virag July 18, 2022

Sorry for the late reply @Andrea Pannitti I only now had a chance to test. I put the script in the create transition, and while

"passesCondition": "false (java.lang.Boolean)"

ticket still gets created?

Thank you

Suggest an answer

Log in or Sign up to answer