Compare two dates and make a field mandatory upon result using behavior Edited

Hi, 
I wrote a script for behavior to get ticket's due date and the current date of the system.

If due date passed then a custom field should be required on transition screen when going from any status to close. 

Here's my script:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.text.SimpleDateFormat
com.atlassian.jira.issue.issuetype.IssueType
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
FormField dueField = getFieldByName("duedate")
FormField cfValue = getFieldByName("DTD - Root Cause Category")
String timeStamp = new SimpleDateFormat("dd/MMM/YYYY").format(new Date());

String dateGA=dueField.getValue()
Date newDate1=Date.parse("dd/MMM/YYYY",dateGA)
Date newDate3=Date.parse("dd/MMM/YYYY",timeStamp)
if(newDate3.before(newDate1))
{
cfValue.setRequired(true)}

 

There're no error, but the results are not as expected. 

Any ideas?

Thanks!

1 answer

0 vote
Alexey Matveev Community Champion Dec 21, 2017

You should change your script to

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import java.text.SimpleDateFormat
com.atlassian.jira.issue.issuetype.IssueType
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
FormField dueField = getFieldById("duedate")
FormField cfValue = getFieldByName("DTD - Root Cause Category")
String timeStamp = new SimpleDateFormat("dd/MMM/YYYY").format(new Date());

String dateGA=dueField.getValue()
Date newDate1=Date.parse("dd/MMM/YYYY",dateGA)
Date newDate3=Date.parse("dd/MMM/YYYY",timeStamp)
if(newDate3.before(newDate1))
{
cfValue.setRequired(true)}

The changed line is FormField dueField = getFieldById("duedate"). All system fields you should reference by getFieldById

And your script should be attached to the DueDate serverside script (You need to add DueDate field in your behaviour and attach the script to the field) 

Hi Alexey, 

Thank you for your answer.

I follow your suggestions and when duedate had passed (compare to current date) the behavior is not working as expected. The custom field is not required on transition. 

Any suggestions? 

Alexey Matveev Community Champion Dec 27, 2017

Log values for your fields and see what s wrong.

add log.error(newDate1) and log.error(newDate2). Then have a look what is logged in the atlassian-jira.log

I found the following line in jira.log:

fieldId: duedate, file: <inline script>
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Alexey Matveev Community Champion Jan 03, 2018

I think the problem is with

Date newDate1=Date.parse("dd/MMM/YYYY",dateGA)

try to put log.error(dateGA) before the mentioned above line and see what is in the log.

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
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

243 views 0 6
Read article

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