Need help comparing 2 date fields in a workflow?

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

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.

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

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!

What does the <= 0 stand for?

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.

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?

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

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

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!

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?

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?

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

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 :)

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

You're welcome yet again!

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

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!

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

232 views 1 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you