Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy Scripted Field to compare dates

Normann P_ Nielsen _Netic_
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.
May 30, 2016

I have a scripted Field (Scriptrunner) to compare dates - the Script is with Template "Numbers".

I should compare 2 dates and return the difference in minutes....

The "Incident Start" field is a "Date Time Picker" type

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField


long createdDateTime = issue.created.getTime()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField IncidentStartField = customFieldManager.getCustomFieldObject("customfield_10091");
long IncidentStart = issue.getCustomFieldValue(IncidentStartField).getTime();


// Return in Minutes
return (createdDateTime-IncidentStart)/60

This actually works for some issues (even though a preview says: 

  • Script83.groovy:14 [Static type checking] - Cannot find matching method java.lang.Object#getTime(). Please check if the declared type is right and if the method exists. Possible solutions: getAt(java.lang.String) @ line 14, column 24.

But It does return correct in some cases... but for:

 

Created=30-05-2016 08:28

Incident Start=29-05-2016 04:45

 

It returns 1,663,666.667


Anyone with skills or a existing data/time compare script ...

 

 

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
adammarkham
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.
May 30, 2016

To get rid of the preview error you could try:

long IncidentStart = (issue.getCustomFieldValue(IncidentStartField) as Date).getTime();

You also need the number searcher for the scripted field: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_tips_tricks_and_gotchas

Then use the following to cast appropriately as the number searcher expects a Double: 

return ((createdDateTime-IncidentStart)/60) as Double
Normann P_ Nielsen _Netic_
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.
May 30, 2016

For sure an improvent with:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.text.SimpleDateFormat	
long createdDateTime = issue.created.getTime()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField IncidentStartField = customFieldManager.getCustomFieldObject("customfield_10091");
// String IncidentStart = issue.getCustomFieldValue(IncidentStartField)
long IncidentStart = (issue.getCustomFieldValue(IncidentStartField) as Date).getTime();
if (IncidentStart)
{
  // Return in Minutes
 
  return (createdDateTime-IncidentStart)/60000 as int
}
else
{
  return 0
}

Seems to Work 

JamieA
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.
May 30, 2016

Date.getTime() gives you the date in milliseconds since the epoch, so as you've seen dividing by 60 is not correct....

0 votes
Normann P_ Nielsen _Netic_
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.
May 30, 2016

Furthermore, adding compare to the script like:

// Return in Minutes
if (IncidentStart < createdDateTime)
{
  return (createdDateTime-IncidentStart)/60
}
else
{
  return 0
}

 

gives:

 

The indexer for this field expects a java.lang.String but the script returned a java.lang.Integer.

This can be solved with

return "0"

But I need a number to compare with in JQL


TAGS
AUG Leaders

Atlassian Community Events