Hi!
I'm trying to calculate the time the issue has been in status Open with a scripted field using Scriptrunner, which is the initial status for the issue.
I've used the example found on this page, which works fine for all other statuses: https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html#_total_time_this_issue_has_been_in_progress
However, as the status I want to calculate is the first one, the code doesn't work. I've tried to use the code from the answer found here: https://community.atlassian.com/t5/Product-Apps-questions/scripted-field-total-time-a-issue-has-been-in-open-state/qaq-p/374214
This code works fine, but as soon as I switch to the next status, the field goes blank. If I go back to Open, it stays blank. Something needs to be changed in the code below, but I don't know what could fix this. Does anyone have a good solution for this?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def inProgressName = "Open"
List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each {ChangeItemBean item ->
def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == inProgressName) {
rt << -timeDiff
}
if (item.toString == inProgressName){
rt << timeDiff
}
}
if (!changeItems) {
rt << (System.currentTimeMillis() - issue.getCreated().getTime())
}
def total = rt.sum() as Long
return (total / 1000) as long ?: 0L
Any help would be appreciated!