Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get the time in minutes, or hours since a custom field was updated?

Tom J February 22, 2021

I'm using Jira 8 with scriptrunner to automate some transitions in our ticket system. I have a listener event that needs to transition automatically if an update event is detected and all fields have been updated within a certain amount of time.

 

I have two fields that I care about how long it's been since they were updated, and three other fields that I don't care when they were updated, just that they are correct. I'm having trouble finding out how to calculate the time since the custom fields were updated last that I can use to determine whether to transition the issue.

 

Can anybody help me with the right syntax to get the time since the last field update?

 

I found this answer https://community.atlassian.com/t5/Jira-questions/Change-from-quot-hours-quot-to-quot-Days-Hours-Mins-quot-in-a/qaq-p/803110)

 

and this question https://community.atlassian.com/t5/Answers-Developer-Questions/Is-there-a-way-to-retrieve-the-date-time-a-field-was-last/qaq-p/549129 that allow me to get access to the date of the last update, but I don't know to turn that into a usable time I can use to transition the issue. Thanks for any help!

1 answer

1 vote
PD Sheehan
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.
February 23, 2021

Here is a short script (console) to demonstrate how to get epoch millis value as a standard base for comparing created field from the changeHistory against current date/time

import com.atlassian.jira.component.ComponentAccessor
def im = ComponentAccessor.issueManager
def hm = ComponentAccessor.changeHistoryManager
def issue = im.getIssueObject('JSP-1922')

def assigneeChanges = hm.getChangeItemsForField(issue, 'assignee')
def lastAssigneeChangeDt = (assigneeChanges) ? assigneeChanges.last().created : null
if(lastAssigneeChangeDt){
log.info lastAssigneeChangeDt
log.info new Date().toTimestamp().time
log.info new Date().toTimestamp().time - lastAssigneeChangeDt.time
} else {
log.info "assignee was never changed"
}

This will give you a number of milliseconds since the change. Simple arithmetics can get you hours and minutes

Tom J February 24, 2021

Great! That seems to do what I wanted it to. Although there's some weirdness in the static checking in Jira 8, but the code runs just fine.

PD Sheehan
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.
February 24, 2021

Yeah, that's because of my sloppy null protection on lastASsigneeChangeDt. The static type checking errors can't know for such what type that's going to be.

Here is an equivalent example without static type checking errors:

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
def im = ComponentAccessor.issueManager
def hm = ComponentAccessor.changeHistoryManager
def issue = im.getIssueObject('JSP-1922')

def assigneeChanges = hm.getChangeItemsForField(issue, 'assignee')
if(assigneeChanges){
def lastAssigneeChangeDt = assigneeChanges.last().created
log.info lastAssigneeChangeDt
log.info new Date().toTimestamp().time
log.info new Date().toTimestamp().time - lastAssigneeChangeDt.time
} else {
log.info "assignee was never changed"
}

Suggest an answer

Log in or Sign up to answer