How to add to scripted field for days open so that it stops accruing after issue is closed

Beverly Braham-Durica
Contributor
April 9, 2021

Hi Everyone,

I've got a field for Days Open scripted and it works great - but I need a way to add to the scrip in order to stop the days accruing once an issue is closed.

Here's the current script

def durationMillis = new Date().time - issue.created.time
def days= Math.round((durationMillis / 1000 / 60 / 60/ 24) as Double)

What script can I add to make this stop the accrual?

(I've mentioned it before - I'm new to scripting in my admin work and am trying to learn more - this one is definitely evading me again)

2 answers

1 accepted

0 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2021

Hi @Beverly Braham-Durica ,

two questions please:

1) what is the definition of closed issue in your case? Issue is in specific status or has resolution set or ??? 

2) if issue is closed, which value should be in the field? 0?

Thank you.

Beverly Braham-Durica
Contributor
April 12, 2021

Hi @Hana Kučerová ,

The definition of closed in the case of this project is an issue that is in the status "Closed"

It would be great is the field stayed at the amount that it was at the time of close and not zero out.

Thank you!

Beverly

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2021

Hi @Beverly Braham-Durica ,

thank you. What if issue is reopened and closed again later - which date should we use in the calculation?

If the time of close is not available as a stored value in the issue (as a custom field's value), we will need to go through the history of issue and find the time of close there, which is much more complicated then just compare two available dates.

Can you imagine preparing such a custom field?

Beverly Braham-Durica
Contributor
April 12, 2021

@Hana Kučerová  - I just double checked with the team who has the project with this field in question and they are totally find with a reset when reopened. Based on process it should be rare and if reopened they will want to see reopen to close time separately.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 14, 2021

@Beverly Braham-Durica thank you. It makes sense to me to store these dates (date of reopen and date of resolution) to the issue to be able to use them in the calculation instead of try to calculate it from the history every time. Can you imagine preparing such a custom fields?

Beverly Braham-Durica
Contributor
April 14, 2021

I can definitely imagine it, but I'm not sure of the coding I need to add to the scripted field - I've not found anything in my searches that matches that final piece that says "stop accruing when closed" 

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2021

@Beverly Braham-Durica Please try something like this

I assumed, that the reopen means, that issue is transitioned to the status Reopen.

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

import static com.atlassian.jira.issue.IssueFieldConstants.STATUS

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

long start = issue.getCreated().getTime()
List<ChangeItemBean> changeItemsForField = changeHistoryManager.getChangeItemsForField(issue, STATUS)
changeItemsForField.each {ChangeItemBean changeItemBean ->
if (changeItemBean.toString == "Reopen") {
start = changeItemBean.getCreated().getTime()
return
}
}

long end = new Date().getTime()
changeItemsForField.each {ChangeItemBean changeItemBean ->
if (changeItemBean.toString == "Closed") {
end = changeItemBean.getCreated().getTime()
return
}
}

return end > start ? Math.round(((end - start) / 1000 / 60 / 60 / 24)) : 0
Beverly Braham-Durica
Contributor
April 15, 2021

@Hana Kučerová - Thank you for this, however, I'm getting an error on the last line: 

Script error.jpg

Is there a different character needed?

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2021

@Beverly Braham-Durica Please try to change last line to:

return end > start ? Math.round(((end - start) / 1000 / 60 / 60 / 24) as Double) : 0
Beverly Braham-Durica
Contributor
April 15, 2021

That worked to clear the error but it's still not showing the correct number of Days between open and closed. I changed status from "Reopen" to "In Progress" as that is the status that it would reopen to. (btw - thank you so much for the help!!)

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

import static com.atlassian.jira.issue.IssueFieldConstants.STATUS

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

long start = issue.getCreated().getTime()
List<ChangeItemBean> changeItemsForField = changeHistoryManager.getChangeItemsForField(issue, STATUS)
changeItemsForField.each {ChangeItemBean changeItemBean ->
if (changeItemBean.toString == "In Progress") {
start = changeItemBean.getCreated().getTime()
return
}
}

long end = new Date().getTime()
changeItemsForField.each {ChangeItemBean changeItemBean ->
if (changeItemBean.toString == "Closed") {
end = changeItemBean.getCreated().getTime()
return
}
}

return end > start ? Math.round(((end - start) / 1000 / 60 / 60 / 24) as Double) : 0Days Open.jpg

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 17, 2021

@Beverly Braham-Durica would you please share the history of the issue, when exactly all the transitions were performed?

0 votes
Mehmet A _Bloompeak_
Atlassian Partner
April 25, 2021

Hi @Beverly Braham-Durica ,

As an alternative you can try Status Time Free app developed by our team. It displays status duration on issue detail page as below. Hope it helps.


stf-issue-screen-view-2.png

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.3
TAGS
AUG Leaders

Atlassian Community Events