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
Hello @Alvin Parreno
Try to define startDateValue and dueDateValue as a Date.
def startDateValue = issue.getCustomFieldValue(startDateField) as Date
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)));
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Yogesh Mude , how about the errors? how can I resolve those? thanks
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.