Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,678
Community Members
 
Community Events
165
Community Groups

Resolution Time in Days

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

0 votes
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

Hi Andy,

Thanks a lot, this worked very well.

Cheers!

Andy Rusnak Atlassian Team Jun 01, 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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you