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,297,338
Community Members
 
Community Events
165
Community Groups

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

1 vote
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.

Like Dave Liao likes this

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
Community showcase
Published in Jira Service Management

Coming Soon: Insight Changing to Assets

The 2020 acquisition of Mindville added powerful asset and configuration management capabilities to Jira Service Management in the form of Insight. Following the completion of that integration, custo...

44 views 0 1
Read article

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