How to Script a Validator for a Date Time Picker Field

David Willox July 25, 2016

Hi Guys,

I am looking to script a validator to allow transition only if the time value in a date/time custom field is within a specified range, or alternatively to prevent the transition if the entered time is between 4-5pm. I am unsure how to script this for a custom date/time picker field.

Currently I have written a script using the following language:

(

(cfValues['Environment']*.value.contains("UAT")) &&

((cfValues['Proposed Time of Recycle'].getTimeString() >= '04:00:00 PM') && (cfValues['Proposed Time of Recycle'].getTimeString() << '05:00:00 PM'))

)

But in testing transition is being allowed at anytime.

Please can someone let me know what kind of syntax and script operators I should be using to build this kind of script.

Thanks,

David

2 answers

0 votes
Deleted user November 18, 2017

Hi @adammarkham ,

I have the same requirement here : 

Based on the "hour" value I want to update a select list field "Shift" which has options shift 1, shift 2 etc. 

When I use the following code, I get error when trying to extract the "Time" value from date value.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

def issue = issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// replace name with your field name
def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle")

//def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTimeString())

def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long)
def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY)

// Check hour is not between 4pm to 5pm
if (hour == 16) {
throw new InvalidInputException("DateTime should not be between 4-5pm!")
}

 

The line 

def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long)

Is  showing error :

datetime.png

Could you please help me on this?

 

Thanks

Swathi

adammarkham
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.
November 20, 2017

This is  just a static type checking error, when it runs it will work.

To make the type checker happy, we have to tell it what type the value of the date time field is. Date time fields in JIRA give you a java.sql.Timestamp.

Your script should be like the following:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

import java.sql.Timestamp

def issue = issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// replace name with your field name
def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle")

def dateTime = new Date((issue.getCustomFieldValue(dateTimeCf) as Timestamp).getTime() as Long)
def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY)

// Check hour is not between 4pm to 5pm
if (hour == 16) {
throw new InvalidInputException("DateTime should not be between 4-5pm!")
}
0 votes
adammarkham
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.
July 28, 2016

You can do this by adding a custom scripted validator and  getting the hour from the date like in the following example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

def issue = issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// replace name with your field name
def dateTimeCf = customFieldManager.getCustomFieldObjectByName("Proposed Time of Recycle")

def dateTime = new Date(issue.getCustomFieldValue(dateTimeCf).getTime() as Long)

def hour = dateTime.toCalendar().get(Calendar.HOUR_OF_DAY)

// Check hour is not between 4pm to 5pm
if (hour == 16) {
    throw new InvalidInputException("DateTime should not be between 4-5pm!")
}

I'm not sure what type the "Environment" field is but you need to get the value using the customFieldManager in the same way as the other field and then add the condition to the if statement. The cfValues variable is not available in the binding.

Thanks,
Adam 

David Willox July 28, 2016

Thanks Adam, The Environment field is a select list type. How does that change the script above, I'm assuming the class and and definition is different?

David Willox July 28, 2016

Also I have an input from my team asking if it is possible to define the time value by minutes also.

Thanks again.

adammarkham
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.
July 29, 2016

If its single select its just:

def environmentCf = customFieldManager.getCustomFieldObjectByName("Environment")
def environment = issue.getCustomFieldValue(environmentCf)
 
if (hour == 16 &amp;&amp; enviroment == "UAT") {
    throw new InvalidInputException("DateTime should not be between 4-5pm!")
}

If its multi-select it should be:

def environmentCf = customFieldManager.getCustomFieldObjectByName("Environment")
def environments = issue.getCustomFieldValue(environmentCf)

if (hour == 16 &amp;&amp; "UAT" in environments*.value) {
    throw new InvalidInputException("DateTime should not be between 4-5pm!")

}

To get the minute you need:

def minute = dateTime.toCalendar().get(Calendar.MINUTE)
David Willox July 29, 2016

Thanks again Adam, great advice. Can't tell you how many hours of head banging against the screen you've saved me from.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events