How to Script a Validator for a Date Time Picker Field

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 vote

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 

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?

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

Thanks again.

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)

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

Hi @Adam Markham [Adaptavist] ,

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

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!")
}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

359 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you