Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

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

0 votes
Hana Kučerová Community Leader Apr 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.

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 Apr 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?

@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 Apr 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?

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 Apr 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

@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 Apr 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

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 Apr 17, 2021

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

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

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