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
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 :
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!")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also I have an input from my team asking if it is possible to define the time value by minutes also.
Thanks again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If its single select its just:
def environmentCf = customFieldManager.getCustomFieldObjectByName("Environment") def environment = issue.getCustomFieldValue(environmentCf) if (hour == 16 && 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 && "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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks again Adam, great advice. Can't tell you how many hours of head banging against the screen you've saved me from.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.