Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×I'm creating a scripted field called "Days Open" using scriptrunner.
I need 3 dates for the logic
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
}
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
:) I am glad it helped. Have a nice weekend.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Take a look at this script here to get an idea about how to exclude weekends.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Ravi, it is working fine :)
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.