Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get LAST Closed Date

Tammy Robinson
Contributor
January 16, 2019

I am using the below logic to create a scripted field for Date Closed.  I cannot go back into all of our workflows and add a post function for Date Closed at this point so I am calculating it.  However, some workflows allow users to re-open issues.  Is there a way to capture the LAST closed date? The below code captures the 1st time the status is set to Closed.

 

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.issue.history.ChangeItemBean
import java.text.SimpleDateFormat
import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String closed = changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString == "Closed"}?.getCreated()
Date formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closed)

 

1 answer

1 accepted

0 votes
Answer accepted
Tuncay Senturk
Community Champion
January 16, 2019

Hello Tammy,

  1. You can loop reverse after getting status changes with getChangeItemsForField and return the first "Closed" match. However, I recommend to use ID rather than String.
  2. Simply you can use Enhancer Plugin's Status Date custom field and select "Last" to define the Last Closed Date customfield

Best

Tuncay

Tammy Robinson
Contributor
January 17, 2019

Thanks for the reply, Tuncay.  I do not want to purchase a plugin for this. I thought there might be a simpler way to get the last Closed status.  I just don't know the syntax to the get the last item of the list using the find.  Any ideas? Or how to loop reverse?

Tuncay Senturk
Community Champion
January 18, 2019

Hello Tammy,

Just add reverse()

changeHistoryManager.getChangeItemsForField(issue, "status").reverse().find {it.toString == "Closed"}?.getCreated()
Tammy Robinson
Contributor
January 18, 2019

Thank you so much!  That's worked for me.

Tuncay Senturk
Community Champion
January 18, 2019

You're very welcome Tammy

As I mentioned before, it is wise to use status ID rather than status name.

Best

Khan Muhammad Mahfuzur Rahman
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 10, 2022

Hello Tuncay Seenturk_Snapbytes_

How can I implement on cloud jira about your solution? Please give details.

Tuncay Senturk
Community Champion
February 10, 2022

Hello @Khan Muhammad Mahfuzur Rahman 

JEP has cloud support, here is the link.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events