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:
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 ...
Community moderators have prevented the ability to post new answers.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Date.getTime() gives you the date in milliseconds since the epoch, so as you've seen dividing by 60 is not correct....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.