How do I validate a custom date time is set to at least 3 days ahead of the current time?

Inayat N June 2, 2021

Hi,

I have a Date-Time custom field.  I want to enforce that users set it to at least 3 days ahead of the present time.  For example, if today is January 1st, this field should be set to January 4th or later.

I tried the following Scriptrunner behavior below.  Added it is a server-side script to my custom field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours


def futuredate = new Date().plus(3)

def dateFld = getFieldById(fieldChanged)
def dateValue = dateFld.value as Date
dateFld.clearError()

if (dateValue < futuredate){
dateFld.setError("Requested Date cannot be before (${futuredate})")
}

It does not have a compile error, but does not enforce the behavior I want.  Seems like nothing happens.   Any ideas how to fix my script?

1 answer

1 accepted

3 votes
Answer accepted
Tye Joe Wai June 4, 2021

I tested the script that you provided as a server-side script Behavior for a Date/Time picker custom field, but it worked fine for me. Do give this one a try and let me know how it goes :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

def date2 = new Date().plus(3)

def dateField = getFieldById(fieldChanged)
def dateValue = dateField.value as Date

if (dateValue < date2){
dateField.setError("Date cannot be earlier than (${date2})") 
}

It should fulfill the same requirement but is a more simplified version.

Inayat N June 4, 2021

@Tye Joe Wai .  Thanks.

So when I tried this script, it correctly throws the error message when I pick an invalid date.   However, when I fix the date, it still shows the error message and won't let me proceed.

I tried it with both Date and Date-Time fields with the same results.  Any idea how to clear the error when the date is fixed?

Inayat N June 4, 2021

Fixed it by adding this after the def statements.

dateField.clearError()
Like Vikrant Yadav likes this
Sheri Widler January 6, 2024

I have a date time field that during creation, if it is after 830am CST we will not allow that date to be set the same day .  Any thoughts on how to validate that in a workflow create step?  

Suggest an answer

Log in or Sign up to answer