It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Time in Status - Script Runner - custom scripted field - Reset on status change Edited

The following code and variations has been circulating around on how to calculate a time in the template "duration" format for an issue. I have the code working but I believe it is setup to calculate a cumulative total of any time the issue has been in a status during it's lifecycle.

I'm attempting to create a version of this code that will only show the current instance of being this status (any status). So for instance:

  • Issue is created ->
  • Open (returns time in this instance of Open) ->
  • In Progress (returns time in this instance of In Progress) ->
  • Open (returns time in this instance of Open not adding the time from any previous instance)
  • In Progress (returns time in this instance of Open not adding the time from any previous instance)

Here is the code being utilized:

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def currentStatusName = issue?.status?.name

def rt = [0L]
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {item ->

def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == currentStatusName) {
rt << -timeDiff
}
if (item.toString == currentStatusName){
rt << timeDiff
}
}
def total = rt.sum() as Long
return (total / 1000) as long ?: 0L

 I would like this data to retain the duration template if at all possible.

My thought is I need to get the last status change time here somehow, and eliminate the second if statement?

Thank you in advance!

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence

2019: All Wrap(ped) Up Like a Present

Hindsight might be 2020 but looking back, a lot of cool new features rolled out in 2019. From new collaboration and organizational powers to fresh templates and handy integrations, it’s been quite th...

1,038 views 4 8
Read article

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