Resolution Time in Days

Vishal
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 18, 2022

Hi Guys,

I need to calculate the time taken to resolve the issue in days, I am trying to do this using scriptrunner post function & store the value in another custom field, lets say the field Resolution in Days. 

Here is the code that I am using but its not working, Can someone check whats wrong with it ?

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.*
import com.onresolve.jira.groovy.user.FormField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

def resolutionDate = issue.getResolution();
def createdDate = issue.getCreated();

def RDate = resolutionDate.getValue() as Date
def SDate = createdDate.getValue() as Date

def ResolutionDays = getFieldById("customfield_xxxxxx")

ResolutionDays.setFormValue(RDate - SDate)

 

1 answer

1 accepted

1 vote
Answer accepted
Andy Rusnak
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 20, 2022
Hi vishal,

Thanks for using the Atlassian Community!

If I understand your issue correctly (and please correct me if I am wrong), I wrote up a quick example script that will calculate the difference between two date fields and assign that to a custom field within a post-function. I tested this quickly and it looks to work in my environment. I adapted this code from the Adaptavist library for this simple example. You will likely need to customize this for your specific environment and case, but I hope this might at least get you started:

import com.atlassian.jira.component.ComponentAccessor
import java.time.temporal.ChronoUnit

// Name of the custom field to put the result into
final customFieldName = "My Text Field";

// Get the Date values you would like to compare
// In case, we are comparing the resolution date and the
// date an issue was created
final resolveDate = issue.getResolutionDate();
final createDate = issue.getCreated();

// Find the custom field and validate it is on the issue
def customFieldManager = ComponentAccessor.customFieldManager;
def customField = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldName };
assert customField: "Could not find custom field with name $customFieldName"

// Convert the dates into an instants for subtraction algorithm
// Additional documentation about date instants can found here
// https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html

def resolveDateInstant = resolveDate.toInstant();
def createDateInstant = createDate.toInstant();

// Set the field value using the ChronoUnit library to set the difference between the instants
// as days

issue.setCustomFieldValue(customField, ChronoUnit.DAYS.between(createDateInstant, resolveDateInstant).toString() + ' Days');

Others from the Community potentially more familiar with ScriptRunner may be able to offer additional insight or customization to this, but please let me know if you have any questions related to this quick example and I would be happy to answer as best I can.

Best,
Andy
Vishal
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 1, 2022

Hi Andy,

Thanks a lot, this worked very well.

Cheers!

Andy Rusnak
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 1, 2022

Thats great to hear!  Happy I was able to help.  

Take care, 

Andy

Like Vishal likes this

Suggest an answer

Log in or Sign up to answer