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)
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');
Hi Andy,
Thanks a lot, this worked very well.
Cheers!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thats great to hear! Happy I was able to help.
Take care,
Andy
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.