Set Issue Label using last Status Name


4 answers

Hi David,

Based on the only requirement below of "Set Issue Label using last Status Name", i have enclosed a
simple script below which will get the previous status the issue was in and set it as a label on the issue.

Please note that the code must be placed as the first function in the list of post functions in order to get the previous
status and if it is placed after the Get current Destination Step post function then it will return the current status.

* @Setup Notes:  This piece of code should be configured as a 
* script function and placed first in the list to get the previous status.
* @Setup Notes:  If this code is placed after the Get current Destination
* Step post function then it will return the current status.

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue

// Get the current logged in user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()//Get a to 

// Get a pointer the current Issue
MutableIssue myIssue = issue

// Get access to the Label Manager
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)

// Get the last status and replace any spaces with a - character as labels must be a single word
def previousStatus ="\\s","-").toString()

// Check if the Issue has just been created and add a label only if the issue was not previously in the Create status.
if (previousStatus != "Create") {
// Set a Label with the previous Status Name on the issue
    labelManager.addLabel(user,, previousStatus, true)

// Save the issue

I hope this helps.



Hi, I'd be interested in that topic too. Could you please precise which kind of post-function we must use? Am i correct by understanding that we copy/paste the script above in the description box of the post-function selected? Thanks

This should be placed as a custom script post function on the transition of the workflow that you want it to execute for. For example if you have a transition going fro To Do -> In Progress then placing it on the transition in between those statuses then it will get the previous transition of To Do and set it to the label. It must be placed as the first post function in the list to get the previous status. Kristian

0 votes

There's only half a question here...

The Script Runner documentation here shows how to get the previous workflow status and this code sample could be extended so you set the returned value as a label on the issue.



Hi thanks for your prompt answer.

could you just precise where I can write the custom script? which post function, among the ones in the attached screen shot, do I have to select to feed it with the custom script.

I get it that I have to select the transition between 2 status, then the post function: but there several post-function types :

Many thanks



Hi Mel, It looks as if you do not have the Script Runner add on installed. When you install Script Runner you will see a Script Post Function option and a Custom Script Post function below that. Kristian

No unfortunetaly I don't have it... but I asked for it (cross fingers!) ok thanks for your help anyway melissa

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

412 views 0 3
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