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

End date must be greater than start date

Alvin
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.
October 15, 2018

Hi All, I need help on creating a script that verifies if the end date cannot be selected before start date

I have these 2 Date time picker custom field named Start Date and End Date. I need to put validation using scriptrunner . can someone help? Thank you

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.web.action.util.CalendarResourceIncluder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def startDateField = customFieldManager.getCustomFieldObjectByName("Impact Start")
def startDateValue = issue.getCustomFieldValue(startDateField)
def dueDateField = customFieldManager.getCustomFieldObjectByName("Impact End")
def dueDateValue = issue.getCustomFieldValue(dueDateField)
Calendar calendar = Calendar.getInstance(TimeZone.getDefault())
log.warn "starDateValue ${startDateValue}"

calendar.setTime(startDateValue)
int startDateDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
log.warn "starDateValueweek ${startDateDayOfWeek}"
calendar.setTime(dueDateValue)
int dueDateDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)


// validation due date
if (dueDateValue != null ){
if(startDateValue > dueDateValue){
invalidInputException = new InvalidInputException("Due Date must be greater than Start Date")
}
}


// validation start date
if (startDateValue != null){
if(startDateValue > dueDateValue){
invalidInputException = new InvalidInputException("Due Date must be greater than Start Date")
}
}


// validation duedate
if (dueDateValue) {
if (issue.dueDate?.before(Calendar.getInstance().getTime())) {
invalidInputException = new InvalidInputException("Due date must be in the future.")
}
}

 

it throws these errors

 

Screenshot from 2018-10-16 14_14_02.pngScreenshot from 2018-10-16 14_13_17.png

2 answers

0 votes
LUIS MIGUEL GALAN ALONSO October 17, 2018

Hello @Alvin Parreno

 

Try to define startDateValue and dueDateValue  as a Date.

def startDateValue = issue.getCustomFieldValue(startDateField) as Date

0 votes
Yogesh Mude
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.
October 16, 2018

Hi @Alvin

Already you have been fetched the values of StartDateFieldValue and EndDdateFieldValue now chk that both field value should not be empty.

if(EndDataFieldValue > StartDateFieldValue) 
{

flag = true;

}
if(flag)
{
return true;
}
else
{
throw new InvalidInputException("The " + ComponentAccessor.getCustomFieldManager().getCustomFieldObject(new Long(XXXX)) + " should be greater than " + ComponentAccessor.getCustomFieldManager().getCustomFieldObject(new Long(XXXX)));
}
Alvin
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.
October 16, 2018

Hi @Yogesh Mude , how about the errors? how can I resolve those? thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events