Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Script Total Time In Status

Deleted user Feb 09, 2023


I'm using the ScriptRunner script for getting the Time in a particular status.  I am able to get the times for all other statuses except the first status after the issue is opened, in this case Open.  Here is the script I'm using:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

// Status to be counted
final statusName = 'Open '

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def totalStatusTime = [0L]

// Every status change is checked
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each { ChangeItemBean item ->
    def timeDiff = System.currentTimeMillis() - item.created.time
    // Subtract time if the "from" status is equal to the status to be checked and from and to statuses are different.
    // This allows to count the time the issue is in the state for the first time
    if (item.fromString == statusName && item.fromString != item.toString) {
        totalStatusTime << -timeDiff
    // Add time if the "to" status is equal to the status to be checked
    if (item.toString == statusName) {
        totalStatusTime << timeDiff

def total = totalStatusTime.sum() as Long
// Every time (added or subtracted) is summed and divided by 1000 to get seconds
(total / 1000) as long ?: 0L



I'm trying to adjust the script so that I can get the time in the first status which at this point keeps returning a zero.  

Any pointers on how to adjust it to get the duration of the first status the issue transitions into? 


Note: I'm on JIRA Server version v8.20.11 




1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 12, 2023

You've run into something that I think of as a design flaw in the original database.

An issue object is a simple row in a table in the database, holding the current data for it.  That's an intuitive and natural way to hold a representation of an object, the way most humans will think about it.

But it's not the right way to do it for an object that might change.  Instead of "current object" and then a "change history" in a different place, a better design would be to have one row per change and an indicator of "current".  Create issue creates the first row, then edits create new ones.  It's not an efficient use of disk space, but it makes reporting much easier to code for.

Anyway, in the current structure, creating an issue creates the issue object.  There's no history to it, and the history only starts when you first update the issue.

So, you need to catch that.  The first status entry in the change log will contain a "from" status.  You must find that entry, read its date/time, and subtract the issue.created date/time from it (instead of another history log)

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events