Script for script listner to update the label field with a value in a custom field when it changes

 I am new to scripting and I am looking for a script that will update the label field with a value of a custom field anytime the custom field is changed. The script would need to overwrite the label field with the custom field value. I know I can set post functions in a workflow to copy the field value from field to field but I would have to add this at every transition point in the workflow and would rather find a way to have a listener to make the change to the label field if this custom field changes.  Any ideas on an easy way to accomplish this? 

1 answer

Hi Beth,

This is an interesting use case. You can edit label fields directly by simply typing in them to add new lables. If you need something a bit more complex I have written a script below that seems to fit your use case. It will manage the label for you, so that it cleans up after itself when you change the label name.

Script Listener Setup

1. Add a new "Custom Listener" in the Script Listeners in the script runner menu

2. Pick the projects that are applicable and add the event "Issue Updated"

Script

Use the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

// [1] Get the resources we need
def auth = ComponentAccessor.getJiraAuthenticationContext()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def labelMgr = ComponentAccessor.getComponent(LabelManager)

def issue = event.getIssue()
def lastChange = changeHistoryManager.getChangeHistories(issue).last()

// [2] IF the "Label Value" field has been updated
def changeItem = lastChange.changeItemBeans.find {it.getField() == "Label Value"} // TODO - Change this to be the name of your custom text field

if (changeItem) {
def lastLabel = changeItem.getFromString()
def newLabel = changeItem.getToString()

if (lastLabel != newLabel) {
// Someone has changed the label - Let's update the labels field
def labels = labelMgr.getLabels(issue.id).collect{it.getLabel()}

// [a] Remove the old value (if it exists)
labels -= lastLabel

// [b] Add the new value
labels += newLabel

labelMgr.setLabels(auth.getLoggedInUser(),issue.id,labels.toSet(),false,false)
}
}

NOTE: On line 13 as per the comment you need to reference your own custom field that the user will enter the name of their label.

Hope this helps!

Steve

Thank you for putting this together for me.  I have one question though and I should have been more clear in my question, but what if the label is updated from a single select drop down field instead of a label field?  The field I am pulling the value from is actually a single select drop down and the label field needs to be updated to whatever is selected in that custom single select drop down field.  The whole reason I am trying to do this is because Tempo uses the label field for issues that may be capex, opex, finex and to keep a consistancy in what is added to the label field as far a spelling and letter case we are updating the label field from a pick list value and I have this set up as a post function using the copy from field to field.  This only does the action one time and to have it update on the fly I would need a listener instead of adding the post function to every transition point in the workflow.  The actual label field has been hidden and is not used for anything but this scenario.

Hi Beth,

That's a good question. I've just tested it with a single select dropdown and it works in the exact same. You won't require any change to that script for that condition.

Hi Beth,

Did this solution work out ok for you?

This worked out perfectly.  I am assuming this same concept can be used to copy from one field to another in other scenarios such as one custom field to another?  If yes, then how do I change the script to find the cf value?

Hi Stephen,

Thank you for providing this ScriptRunner Listener code. I needed a Listener to add a specific label to an issue when a specific Custom Field was updated to Yes; Options are None, Yes, No.

I used your code and changed it a bit so that a Label is added to an issue when a Custom Field (Select List) option is changed to Yes via an Issue Update event. I commented out the code to remove the label if one existed. 

I added my code below for any user who needs to add a label to an issue based on a Custom Field Update event.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

// [1] Get the resources we need
def auth = ComponentAccessor.getJiraAuthenticationContext()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def labelMgr = ComponentAccessor.getComponent(LabelManager)

def issue = event.getIssue()
def lastChange = changeHistoryManager.getChangeHistories(issue).last()

// [2] IF the "Your Custom Field ..." field has been updated
def changeItem = lastChange.changeItemBeans.find {it.getField() == "Your Custom Field"} // TODO - Change this to be the name of your custom text field

if (changeItem) {
def lastSelection = changeItem.getFromString()
def newSelection = changeItem.getToString()

if (newSelection == "Yes") {
// Someone has changed the selection - Let's update the labels field
def labels = labelMgr.getLabels(issue.id).collect{it.getLabel()}

// [a] Remove the old value (if it exists)
//labels -= lastLabel

// [b] Add the new Label
labels += "your-label"

labelMgr.setLabels(auth.getLoggedInUser(),issue.id,labels.toSet(),false,false)
}
}

Thank you again Stephen. Your response is greatly appreciated, and Thank You Beth for asking the question.

Best,

Joe

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...

166 views 0 5
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