Need help comparing 2 date fields in a workflow?

Warren McInnes
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.
July 3, 2013

Hi,

I have a workflow where on one of the transitions we need to check two date fields and either allow the transition or deny it.

we need this type of comparision:

"My custom field/future date" > CurrentDate()/today's date

So therefore, If the future date is larger than today's then allow transition, if it is not then give an error?

Is there anyway to achieve this? Script runner perhaps?

Thanks,

Warren

3 answers

1 accepted

4 votes
Answer accepted
Justin Alex Paramanandan
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.
July 3, 2013

You can install the Script Runner plugin and add a validator to the transition you've mentioned with the following settings:

Validator type: Script validator

Script:

Date now = new Date()
Date cfDate = new Date(cfValues['DateFieldA'].getTime())
new Date(now.getYear(), now.getMonth(), now.getDate()).compareTo(cfDate) <= 0

Replace DateFieldA with your Date Picker custom field's name.

*updated to reflect the comments of this answer.

Warren McInnes
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.
July 3, 2013

Would this same script you have provided work in the script-runner by Jamie Echlin?

Justin Alex Paramanandan
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.
July 3, 2013

Ah, my bad! The plugin is the Script Runner plugin: https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner

But yes, that's the plugin I used in conjunction with the script. I've updated my answer above to the correct plugin. Thanks for pointing that out!

Warren McInnes
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.
July 3, 2013

What does the <= 0 stand for?

Justin Alex Paramanandan
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.
July 3, 2013

The <=0 is a comparison operator that is similar to Java's. I'll break down the code line for you:

  • new Date(now.getYear(), now.getMonth(), now.getDate()) will get the current date.
  • .compareTo(cfDate) is the comparison method; we pass the date custom field here.
  • <= 0 means that the comparison method would return a negative value if the value that invokes the method (new Date calling the .compareTo() method) is lesser than the value being compared to (cfDate).

In short, if the current date is earlier than the date set in the cutom field, this script will return true; hence allowing the transition to proceed.

Warren McInnes
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.
July 3, 2013

Excellent I see, However i am now getting this error when i try start progress

Could not load FunctionProvider class

It seems that you have tried to perform an illegal workflow operation.

If you think this message is wrong, please contact your JIRA administrators.

I'm and using the feature the produce periodic tasks, so by using the script runner's escaltion service i can bring the task into an active status by running the JQL "My custom field/future date" = StartOfDay()

However we need to be sure people don't set it to a later day when setting "My custom field/future date", but for some reason am getting this error? can you advise?

Justin Alex Paramanandan
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.
July 4, 2013

Hmm, can you show what are your post functions for the Start Progress transition?

Also, it would be cool if you can let me know:

  • the version of your JIRA;
  • the version of the plugin;
  • the custom field name
Warren McInnes
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.
July 4, 2013

Nothing on the Start Progress tranition, just the default post-functions.

  • Jira 5.2.1
  • Script Runner 2.1.3
  • I am calling my customfield for "My custom field/future date" , Recurrence Date

Thanks, Hope this helps

Justin Alex Paramanandan
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.
July 4, 2013

Ok, so your custom field's name is "My custom field/future date", yea? Also, it's a Date Picker type field?

When you try to add the script, did you choose the Script Validator option:

And then choose Simple scripted validator:

And finally add the script mentioned above in the Condition code box:

If all have been added, it should be like the following:

The above worked for me here. Let me know how this goes for you, mate!

Justin Alex Paramanandan
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.
July 4, 2013

No problem, Warren! Glad that it works now. :)

But it is indeed a strange thing that a script runner for the escalation is affecting this script. So, does this mean that both scripts are working well?

Warren McInnes
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.
July 4, 2013

It's called "Recurrence Date" and it is a Date Picker type field

This is my workflow:

As you can see, we use this to minpulate this as a task type for recurring tasks, and it's been working well. By using the Escalation sevice by script runner once "Recurrence Date = StartOfDay()" than it automatically transitions from "Deactive" into "Active", The Recurrence Date is set on completing the Task, thus transitioning it from "Active" to "Deactive", How ever at this point we need to make sure that the user cannot set the reccurence date older than the current date aka today's date.

He is my workflow step with your code:

So as you can see i want them to set the reccurence date when completing the issue and at this piont also not allowing them to insert a date older than today?

Warren McInnes
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.
July 4, 2013

Ok Seems like it's working, THANK YOU JUSTIN SO VERY MUCH for all your help! I think there was another function that was causing the problem, but it seems your code is working well! Thank you

Warren McInnes
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.
July 4, 2013

No no the escalation service isn't affecting your script, it seems it was another plugin i was using that i seemed to have disabled, but when transition ran is was still trying to find the function in the disabled plugin. After removing the function your script ran well.

Your script runs well with the escalation service! Thank you :)

Justin Alex Paramanandan
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.
July 4, 2013

Ah, I see. Glad that you got that part sorted out, mate! :D

You're welcome yet again!

2 votes
Alex Perez
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.
July 3, 2013
Warren McInnes
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.
July 4, 2013

Hi Alex, I did try this approach, however it doesn't support a custom field

0 votes
Nitram
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.
July 3, 2013

Hi,

This can be done, Write a workflow condition plugin which will help you to do this, look into this link, which may help you,

http://www.j-tricks.com/1/post/2010/08/workflow-condition.html

Hope this helps!

Warren McInnes
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.
July 3, 2013

Hi Nitram, This option looks very time consuming and may be a bit difficult?

Suggest an answer

Log in or Sign up to answer