Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

End date should not appear before Start Date

Prashant Hiremath September 1, 2018 edited

I have created two custom date picker fields, START DATE and END DATE. These two fields are used on Create Issue Screen.

I am using script runner addon to I validate END DATE against START DATE.

User should not be allowed to choose an End Date that is much before the Start Date.

As an end result, user should be not allowed to create ticket if END DATE chosen is prior to the START DATE

Request help from the Community  !!!

 

Following is the code that I have added in Script Console:

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('Start Date')
def startDateValue     =  issue.getCustomFieldValue(startDateField)
def endDateField     =  customFieldManager.getCustomFieldObjectByName('End Date')
def endDateValue     =  issue.getCustomFieldValue(endDateField)

Calendar calendar      = Calendar.getInstance(TimeZone.getDefault())

calendar.setTime(startDateValue)

calendar.setTime(endDateValue)

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

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



// This block validates end date against start date
if (issue.endDateValue) {
if (issue.endDateValue?.before(Calendar.getInstance().getTime()))
    {
    invalidInputException = new InvalidInputException("End date must be in the future")
    }
}

 

When I run the above code, I get following error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script177.groovy: 22: expecting ')', found ';' @ line 22, column 23. if(startDateValue > endDateValue){ ^ 1 error

1 answer

1 vote
Nic Brough -Adaptavist-
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.
September 1, 2018

You have copied and pasted this script from somewhere without reading it properly.  It's html encoded and you need to convert it back into the original plain-text

Prashant Hiremath September 3, 2018

Thanks, Nic

I have converted the code using html to text converter, still no success

Nic Brough -Adaptavist-
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.
September 8, 2018

Please, do not assume that a converter has done it correctly. 

If you're getting the same error, then your converter does not understood that the content is code and not converted it correctly.  Please write the code properly.

If there is a different type of error, one that is not an encoding error, then we'll need to see what it is, and what the error is.

Suggest an answer

Log in or Sign up to answer