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

Validate Start date is smaller than End date only when the value exists

Uditi Sur July 21, 2022

Hi,

I have two optional fields 'GT Dev Start Date' and ' GT Dev End Date'. I would want it to be optional as in user can choose to skip filling the dates if not needed.

But in scenarios where an user chooses to fill the dates(both Start and End) I would like to validate that the End Date is not smaller than the Start Date.

Could you please help me with a script for the same. I have developed something myself(am still new to scripting) but it makes it mandatory to fill the dates, thereby dissolving the purpose.

Sharing the script- 

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
import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript
import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.core.util.StringUtils

@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()

// Get the Custom Field Manager
CustomFieldManager FieldManager = ComponentAccessor.getCustomFieldManager()
def startDateField = getFieldByName("GT Dev Start Date")
def startDateValue = startDateField.getValue() as Date
def endDateField = getFieldByName("GT Dev End Date")
def endDateValue = endDateField.getValue() as Date
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(endDateValue)
int dueDateDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)


// validation end date
if(endDateValue == null){
return;}
if(endDateValue != null ){
if(startDateValue > endDateValue){
def invalidInputException = new InvalidInputException("End Date must be greater than Start Date")
}
}


// validation start date
if (startDateValue == null){
return;}
if (startDateValue != null){
if(startDateValue > endDateValue){
def invalidInputException = new InvalidInputException("End Date must be greater than Start Date")
}
}

1 comment

Comment

Log in or Sign up to comment
YogeshKR
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 17, 2022

Hi @Uditi Sur

I don't think "InvalidInputException" will works on behaviors.
I've already implemented this in my project.

Please find the script below and hope this may help you,

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

@BaseScript FieldBehaviours fieldBehaviours

def Startdate = getFieldById("customfield_XXXXX") // you can also use it by name - getFieldByName("") 
def Startdatevalue = Startdate.getValue() as Date
def enddate = getFieldById("customfield_XXXXX")
def enddateValue = enddate.getValue() as Date

def Error = "Due Date greater than Start Date" // your error message here

if (enddateValue && Startdatevalue){
    if (Startdatevalue > enddateValue){
        Startdate.setError(Error)
        enddate.setError(Error)
}
    else {
        Startdate.clearError()
        enddate.clearError()
}
}

Kindly let me know in case of any challenges

Regards,
Yogesh

TAGS
AUG Leaders

Atlassian Community Events