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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,457,022
Community Members
 
Community Events
176
Community Groups

Validate date custom field based on current time (ScriptRunner)

Z B Rising Star Dec 09, 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

2 votes
Answer accepted

Hi @Z B ,

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

Z B Rising Star Dec 11, 2019

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

Antoine Berry Community Leader Dec 11, 2019

Glad it helped @Z B :)

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

Like Z B likes this
Z B Rising Star Jan 02, 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 Jan 02, 2020

Hi @Z B ,

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
Z B Rising Star Jan 02, 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 Jan 02, 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
Z B Rising Star Jan 02, 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 Jan 02, 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.

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. 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events