Get Difference Between Start Date and Current Date if Resolution Date is NULL

Christina Hiller June 14, 2019

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
}

 

1 answer

1 accepted

2 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
June 14, 2019

Hi @Christina Hiller 

Try this

if (null !=resolutionDate )

Christina Hiller June 14, 2019

So simple!  Thanks for the help.  Final working script below.

 


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.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"}

// If the resolution Date is NOT NULL, Return difference between Resolution Date and Start Date in Days
if (null !=resolutionDate )
{
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 (today-startDate )/(1000*60*60*24) as int
}
Ravi Sagar June 14, 2019

:) I am glad it helped. Have a nice weekend.

Mujahid Shaik June 11, 2020

Hi Ravi Sagar,

Thanks above script is working fine for me. but my requirement is to exclude weekend (saturday and sunday). will you please help me in achieving it.

Like Ravi Sagar _Sparxsys_ likes this
Ravi Sagar _Sparxsys_
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.
June 12, 2020

Take a look at this script here to get an idea about how to exclude weekends.

Like Mujahid Shaik likes this
Mujahid Shaik June 14, 2020

Thanks Ravi, it is working fine :)

Like Ravi Sagar _Sparxsys_ likes this

Suggest an answer

Log in or Sign up to answer