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

AllieB December 21, 2017

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 votes
Alexey Matveev
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.
December 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) 

AllieB December 27, 2017

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
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.
December 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

AllieB January 3, 2018

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
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.
January 3, 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
TAGS
AUG Leaders

Atlassian Community Events