How to get the previous status of an Issue Edited

I'm currently trying to use Scriptrunner Script Field to keep track of the last status of an issue in JIRA. How do I get the previous status of an issue?

1 answer

1 accepted

0 votes
Accepted answer

Hi Jackson.

You can use te changeHistoryManager like so:

def changeItems = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)

This will contain an arrayList of all the changes that the issue has gone through, including status changes.

My searching capabilities led me to this link, which contains an answer from another Community Champion that confirms it.

Do say if you need anymore help.

Cheers

DYelamos

Just curious now which item in the list returned by getAllChangeItems(issue) is the last status, and which method returns the name of the last status. The documentation on ChangeHistoryItem is less doc more just list of methods.

Ah yes, "documentation" they call it.

As you can see in that documentation, it has a varios set of maps, one that is named FROM and another on that's named TOS. This would seem like the most logical place for the information that you are looking for.

I actually don't have a clue as to what those maps contain, but when I'm dealt bad documentation from our blue friends I usually get an instance of the object in the Script Console of SR and do something like this.

1. Get an issue with the IssueManager.

2. Get your history changes in one object, in this case, let's call it changeItems like before.

3.Then output something like this.

changeItems.getProperties().each{log.debug(it)}

 That way, you have a clear text output of what the object contains.

That should give you some more insight. If you are not self sufficient, I could find out for you if you want.

Do tell me if I can help you further.

Good luck!

DYelamos

For anyone's reference, getFroms() will get the state name (value) and ID (key) in some of the ChangeHistoryItems.

It's easier if you use getChangeItemsForField(Issue, Field) though and then just pull it's 'from' variable. If you're looking for something to get the last status, here it is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager

//Gets List of all changes to 'status' and takes from last one in the list
def changeItem = ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, 'status')?.last()
//variable 'from' is the previous state, 'String' for String type
return changeItem?.fromString

  

Thanks for providing this information. I'm sure it will be very useful to other users.

Cheers!
DYelamos

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,373 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you