I'm creating a scripted field called "Days Open" using scriptrunner.
I need 3 dates for the logic
- startDate (customfield_10444)
- resolutionDate (resolutiondate)
- currentDate
The requirement logic:
If startDate is in future
{return null}
Else IF startDate and resolutionDate are both NOT NULL,
{return #Days between resolutionDate and startDate }
ELSE
{return #Days between currentDate and startDate}
---
My script is working fine when there is a resolution date, however I'm having difficulty getting it to work to compare to the current date when resolution date is not populated. Any help out there from an expert to fix my script??
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
import java.util.Date.*
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField startDateField = customFieldManager.getCustomFieldObject("customfield_10444"); //start date
long startDate = (issue.getCustomFieldValue(startDateField) as Date).getTime(); //start date value
//def resolutionDate = issue.resolutionDate //resolved date
def resolutionDate = issue.getResolutionDate();
def today = (new Date() as Date).getTime() //current datetime
// If StartDate > Today, Return 0 --we don't want to calculate anything not started --how to do this??
//if (startDate > today) {return "0"}
//how to handle situation where resolution date is NULL? If it is not populated, I want to calculate off of current date.
if (resolutionDate !=0 ) // why doesnt it work when i use resolutionDate !=NULL ??
{
// Return difference between Resolution Date and Start Date in Days
long resolutionDateTime = issue.resolutionDate.getTime()
return (resolutionDateTime-startDate)/(1000*60*60*24) as int
}
// Return difference between Today and Start Date in Days
//times returned in calc are in milliseconds - update the formula based on time segment needed
else
{
return (startDate - today)/(1000*60*60*24) as int
}