Custom Date field compared to SystemDate failing

Ashvin Patel February 11, 2020

Hello all,

 

I am trying to compare the Start Date/time Custom field of a Change Request ticket type with the current system date/time.  I want to ensure that if the CR is NOT an "EMERGENCY" change type, the Start Date/Time must be in the future.  

Emergency CR tickets are allowed to be created after the actual work is completed.

I have the following Validator script on the Create transition:

Date currentdatetime = new java.sql.Timestamp(new Date().getTime())

log.warn ("??? - currentdatetime = " + currentdatetime)
log.warn ("??? - Change type = " + cfValues['Change type'])
log.warn ("??? - Planned Start Date = " + cfValues['Planned Start Date'])

if (cfValues['Change type'] != 'Emergency'){ log.warn ("Change type is NOT Emergency")
if (cfValues['Planned Start Date'] != null && cfValues['Planned End Date'] != null) { log.warn ("start and end dates are filled in")
if (cfValues['Planned Start Date'] < currentdatetime ) { log.warn ("Start Date is EARLIER than now")
return false
} else {log.warn ("??? - Start date is NOT before today")
return true }
}
} else { return true}

 

This works just fine when the change type is NOT Emergency type.  however, it is not working as expected when the Change Type (Custom field) is equal to Emergency.  

The output of the script is as follows:

2020-02-12 04:23:37,821 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - currentdatetime = 2020-02-12 04:23:37.821
2020-02-12 04:23:37,822 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - Change type = Emergency
2020-02-12 04:23:37,822 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - Planned Start Date = 2020-02-10 04:23:00.0
2020-02-12 04:23:37,822 WARN [workflow.AbstractScriptWorkflowFunction]: Change type is NOT Emergency
2020-02-12 04:23:37,823 WARN [workflow.AbstractScriptWorkflowFunction]: start and end dates are filled in
2020-02-12 04:23:37,823 WARN [workflow.AbstractScriptWorkflowFunction

 

I have a feeling it's something really simple, but I just can't figure it out.

 

Any and all answers/help is appreciated as always. 

1 answer

1 accepted

0 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.
February 13, 2020

Hi @Ashvin Patel ,

is there a line of log missing ? I cannot see the result of the date comparison. Anyway you could also try (even though it should be equivalent) : 

if (cfValues['Planned Start Date'].compareTo(currentdatetime) < 0){ ...
Ashvin Patel February 17, 2020

There was nothing after that last entry in the log file.  I have tried your suggestion for the compare and it is still not working.

I think the issue is above this date comparison with the very first if statement:

if (cfValues['Change type'] != 'Emergency'){ log.warn ("Change type is NOT Emergency")

This comparison seems to evaluate as True, every time, even though in the logs, it clearly shows that the Change Type field value is Emergency:

2020-02-17 18:49:06,053 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - currentdatetime = 2020-02-17 18:49:06.053
2020-02-17 18:49:06,054 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - Change type = Emergency
2020-02-17 18:49:06,054 WARN [workflow.AbstractScriptWorkflowFunction]: ??? - Planned Start Date = 2020-02-03 18:46:00.0
2020-02-17 18:49:06,055 WARN [workflow.AbstractScriptWorkflowFunction]: Change type is NOT Emergency
2020-02-17 18:49:06,055 WARN [workflow.AbstractScriptWorkflowFunction]: start and end dates are filled in
2020-02-17 18:49:06,056 WARN [workflow.AbstractScriptWorkflowFunction]: Start Date is EARLIER than now
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.
February 19, 2020

If the issue is only when Change type value is Emergency, then your first if is probably not working since logs display 

log.warn ("Change type is NOT Emergency")

Could you try adding this log 

if (cfValues['Change type'] == 'Emergency'){ 
log.warn("Change type is Emergency")
}

Before the other if, and try to trigger the script when Change type value is Emergency ?

Ashvin Patel February 24, 2020

Issue resolved.  The problem was that I was not converting the single-select Drop-down selected value to a String before attempting to compare that value to another string.  

 

I changed :

if (cfValues['Change type'] != 'Emergency'){

to:

if (cfValues['Change type'].toString() != 'Emergency'){

 

and that resolved the issue.

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.
February 24, 2020

Or does it work ? :D

Ashvin Patel February 24, 2020

yes, it works now :). Thank you for your time and assistance.

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.
February 24, 2020

Indeed list (and checkbox) fields will need to be converted to string first for comparison. Anyway if you are satisfied with the resolution please mark the question as answered as it will help others in the future. :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events