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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

365 views 0 6
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