It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Allie Bendor Dec 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 [cPrime] Community Leader 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) 

Allie Bendor Dec 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 [cPrime] Community Leader 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

Allie Bendor Jan 03, 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 [cPrime] Community Leader 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

188 views 2 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you