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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Beth,
Did this solution work out ok for you?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, I'm getting 'Anonymous user changed the label' ; labels gets added though
I'm using:
ApplicationUser user1 = ComponentAccessor.getUserManager().getUserByKey("Jira_Automation")
labelManager.addLabel(user1, issue.id, newlabel, false)
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.