Forums

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

Validation of custom date field and compare custom date fields

Johannes Rudolf
Contributor
June 24, 2020

Hi all,

I have been challenged to

a) validate a date given in a date picker custom field

b) compare two dates within a couple of other date picker custom fields

 

The use cases are (while creating an issue) 

a) students may be able to sign up for services and will have to leave a day of birth - which has to be a reasonable value in the past - it would be plenty to check if the given date is around 20 years in the past (for example)

b) in order to request a machine user an initial time frame of validity for the machine user shall not extend 1 year from "start date" hence the value from "end date" shall not be higher than one year compared to "start date"

 

Both should be done with the help of Script Runner. I went through both the Adaptavist manual and library and also have checked around here:

 

In best case I came close. However for the first use case I had a couple of (very basic) approaches since I'm not very familiar with scripting even though I understand the concept of class imports, defining variables etc. For example I did not understand why I do not have to import classes here (taken from the first link above)? In general I found this one quite useful when changed appropriately (for which I'd appreciate your help.

def startTime = cfValues['Start Time']
def currentDate = new Date()
if (currentDate.getHours()>=12){
currentDate.plus(2).clearTime().compareTo(startTime) <= 0
}
else {
currentDate.plus(1).clearTime().compareTo(startTime) <= 0
}

For instance the first three lines would look like this:

def bday = cfValues['Day-of-Birth']
def currentDate = new Date()
if (currentDate.getYear()>=20)

 

As for the second use case (can we handle this here too?) I have learned that we can calculate the difference. However I was unable to deal with the value then during issue creation. I'm sure there's a way, can someone please show me?

 

Thank you very much in advance

Cheers,

Johannes

1 answer

1 accepted

1 vote
Answer accepted
Ravi Sagar _Sparxsys_
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.
June 24, 2020

Hi @Johannes Rudolf 

Depending upon what you want to do, you will need to import classes. In case of behaviour not always if you just adding logic based on fields on the screen.

In seems like you need to create a behaviour for your requirement to control what user can enter during issue creation.

Ravi

Johannes Rudolf
Contributor
June 24, 2020

Good point, thanks for that. After another research I found this quite useful snippet and after a quick modification I got it running.

// required import statements
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*

// Get pointers to my date field and get its value as a date
def cf = getFieldByName("DayOfBirth")
def cfval = cf.getValue() as Date

// get todays date
def today = new Date()
// get yesterdays date
def yesterday = today.minus(6570)

// get the date three months in the future
//def threeMonthDate = today.plus(90)

// if the date entered is before todays throw an error
if(cfval.after(yesterday)) {
cf.setError("Please enter a plausible date in the past. You must be 18 years old at least.")
}else{
cf.clearError()
}

 

Suggest an answer

Log in or Sign up to answer