This is about a validator in a workflow for the create issue event, a Simple scripted validator.
There is a Start Time which the user has to set on the Service Desk page when opening the ticket. The user can select a Start Time for tomorrow only until 12 noon of the day of opening the ticket. If it's past noon, they can only select a Start Time for the day after tomorrow.
First I tried to compare the Start Time to the current time
import java.sql.Timestamp
def starttime = cfValues['Start Time']
def noticket = new Timestamp((new Date()).getTime())-(1000*60*60*12*1)
starttime > noticket
Or compare them by substraction:
(cfValues["Start Time"].time - (new Date().getTime()) / 86400000)/43200000
Or would it be something like this here?:
Thanks in advance!
Hi @Zita Bagi ,
Kindly try this script snippet :
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
}
Antoine
Hi, this worked, thank you very much for your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad it helped @Zita Bagi :)
If you are satisfied with the answer, please mark it as accepted so it can help others too !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, there now I have one additional question about this. I'd like to validate this date custom field only if a certain value is chosen in a custom field.
I'm using Simple scripted validator and the script below and it doesn't work, the date is always validated regardless of which category is chosen so if the script validator is enabled, I cannot open a ticket, even if no Start Time has to be added based on the category chosen.
Can you please help with this addition?
def startTime = cfValues['Start Time']
def currentDate = new Date()
if (cfValues['category']?.value == 'category 1') {
if (currentDate.getHours()>=12){
currentDate.plus(2).clearTime().compareTo(startTime) <= 0
}
else {
currentDate.plus(1).clearTime().compareTo(startTime) <= 0
}
} else {
startTime=null
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Zita Bagi ,
Please try this updated snippet :
def startTime = cfValues['Start Time']
def currentDate = new Date()
if (cfValues['category']?.value == 'category 1') {
if (currentDate.getHours()>=12){
currentDate.plus(2).clearTime().compareTo(startTime) <= 0
}
else {
currentDate.plus(1).clearTime().compareTo(startTime) <= 0
}
}
true
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, but this actually doesn't work, the date is simply not validated anymore. I managed to enter tomorrow as a startTime even though it's past noon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In this case we will have to debug further, please try this :
def startTime = cfValues['Start Time']
def currentDate = new Date()
def categoryValue = cfValues['category']?.value
log.error("categoryValue : " + categoryValue)
if (categoryValue == 'category 1') {
if (currentDate.getHours()>=12){
currentDate.plus(2).clearTime().compareTo(startTime) <= 0
}
else {
currentDate.plus(1).clearTime().compareTo(startTime) <= 0
}
}
true
And copy what is displayed in the logs.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried it:
2020-01-02 15:15:13,124 ERROR [workflow.AbstractScriptWorkflowFunction]: categoryValue : category 1
But the category is correctly written, I cannot guess what's wrong with the category value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I would advise then to add logs for all cases :
def startTime = cfValues['Start Time']
def currentDate = new Date()
def categoryValue = cfValues['category']?.value
log.error("categoryValue : " + categoryValue)
if (categoryValue == 'category 1') {
log.error("category is category 1")
if (currentDate.getHours()>=12){
log.error("date is in first scenario")
currentDate.plus(2).clearTime().compareTo(startTime) <= 0
}
else {
log.error("date is in second scenario")
currentDate.plus(1).clearTime().compareTo(startTime) <= 0
}
}
log.error("category is not category 1")
true
Play with the category and date field too make sure the correct case is matched every time.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Antoine Berry,
I am looking to validate a custom field 'current date' that tracks current date but i want to only be able to transition to the next status only if the values of the current date field is less than or equal to: 2022-02-28, 2023-02-28, 2024-02-28, 2025-02-28, 2026-02-28.
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.