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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for providing this information. I'm sure it will be very useful to other users.
Cheers!
DYelamos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.