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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Validate date custom field based on current time (ScriptRunner)

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

0 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

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

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

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.

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Trello

Taco Tuesday: Your favorite Trello blog takeaway

Hello friends! From the community that brought you Welcome Wednesday, Throwback Thursday and Friday Fun, welcome to Taco Tuesday, a weekly discussion about all things Trello. The best part? One Tac...

88 views 7 6
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you