Behaviour script to validate the given data/time field which should accept value after one hour from current time

Vignesh August 17, 2016

Behaviour script to validate the given data/time field which should accept value after one hour from current time.

Its custom field name - Planned Implementation Data/Time.

2 answers

1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 26, 2016

Hi Vignesh,

I have attached a sample Behaviours script that I have created on my local JIRA 7.1 instance which takes the value from a custom date picker picker field called DemoTimeField and checks if the value entered is less than 1 hour from the current date and time and if it is presents the user with an error and forces them to enter in a  value greater than 1 hour from the current date and time.

I have attached the code, config and a screenshot of the output below.

Code

//Required Imports
import groovy.time.TimeCategory
import java.util.Date

// Get a pointer to my Date Custom field
def DateField = getFieldByName("DemoTimeField")
Date DateVal = (Date) DateField.getValue()

// Get todays date
def timeIn1Hour = new Date()

// Add 1 hour to todays date
use( TimeCategory ) {
    after60Mins = timeIn1Hour + 60.minutes
}

// Check if the Start Date is before todays date plus 1 hour and if so present the user with an error and clear the value to the field to force the user to enter in a correct value.
if(DateVal.before(after60Mins)){
    DateField.setError("You are only allowed to select a Date and time that is greater than 1 hour from now" + "<br/>" + "Your selected date and time has been cleared to prompt you to select a valid value")
    // Clear the weekday value to make the user re select a new value as well
    DateField.setFormValue(null)
}else{
    // If a valid value is entered clear any errors and accept the value
    DateField.clearError()
}

Config

Screen Shot 2016-08-26 at 15.56.33.png

Output

Screen Shot 2016-08-26 at 15.48.52.png

I hope this helps.

Thanks

Kristian

0 votes
Vignesh August 17, 2016

Planned Implementation Data/Time  value should not be less than one hour from current time.

Suggest an answer

Log in or Sign up to answer