Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Validate date custom field based on current time (ScriptRunner)

Zita Bagi
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.
December 9, 2019

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?:

https://community.atlassian.com/t5/Jira-questions/Is-there-a-Script-Runner-condition-to-check-created-date/qaq-p/306878

 

Thanks in advance!

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2019

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

Zita Bagi
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.
December 11, 2019

Hi, this worked, thank you very much for your help!

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2019

Glad it helped @Zita Bagi :)

If you are satisfied with the answer, please mark it as accepted so it can help others too !

Like Zita Bagi likes this
Zita Bagi
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.
January 2, 2020

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
}
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 2, 2020

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
Zita Bagi
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.
January 2, 2020

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.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 2, 2020

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.

Like Dave Liao likes this
Zita Bagi
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.
January 2, 2020

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.

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 2, 2020

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.

Jagrit Dewan February 2, 2022

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. 

TAGS
AUG Leaders

Atlassian Community Events