Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Behaviour on label field in jira



I would like to add new labels to label field based on the issue type.

I want to use behaviour plugin to do so.

I already applied this script in a post function on first transition in a workflow (after create):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels({it.getLabel()}
labels += 'Incident'



And it works fine.

But I want this functionality be set as a behavior of a field for few issue type (not associated with specific transition of the workflow, and also updated when the issue is moved to the different issue queue-there will be few categories of the labels depending on issue type, like "incident", "service request", "project"). Can you please advise how I can rewrite this code to be useful as a server site script?



3 answers


//Set one value
String newValue = "111222"
Map<Integer, String> option = [(newValue.toInteger()):newValue];//ID and name value

def field = getFieldById("customfield_12021");
field.setFormValue(newValue); //name value
field.convertTo('com.atlassian.jira.issue.customfields.impl.LabelsCFType'); //convert again and fix bug

//Set more value
String newValue1 = "111222"
String newValue2 = "333444"
Map<Integer, String> options = [

def field = getFieldById("customfield_12021");
field.setFieldOptions(options); //ID and name value
field.setFormValue([newValue1,newValue2]); //name value
field.convertTo('com.atlassian.jira.issue.customfields.impl.LabelsCFType'); //convert again and fix bug
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager

def userManager = ComponentAccessor.userManager
def labelManager = ComponentAccessor.getComponent(LabelManager)

def contextIssue = issueManager.getIssueObject(getContextIssueId())

MutableIssue issue = contextIssue
//User to Run function
def anuser = userManager.getUserByKey("user")
//Get the labels for the issue, place them in a list.
def labels = labelManager.getLabels(*.label
//These two are the labels you want to add to your list of labels, if you want to replace them use:
// labels = ['one', 'foo']
labels += ['one','foo']
labelManager.setLabels(anuser,, labels.toSet(), false, false)


Hi, @Matthew Beda  is not working.

java.lang.NullPointerException: Cannot get property 'id' on null object
2020-06-26 16:10:11,163 http-nio-8080-exec-25 ERROR admin 970x129483x1 35kaww,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json

17 line: labelManager.setLabels(anuser,, 12021L, labels.toSet(), false, false); 

contextIssue does not have an identifier

What version of Jira are you using?


I have looked into this issue it turns out that it is not possible to set the value of the Labels field using the Behaviours module of Script Runner. I have enclosed a screenshot from the Script Runner documentation showing that this is not possible.

labels bug.png

A support ticket has been created for this issue can be viewed at GRV-918.

For the meantime I would suggest you keep using the code above using post functions to add labels. You could use the example if statement code in your post function to check if the issue is of a certain type and if so only add labels for that issue type.

// Repeat the if statement for each issue type that needs to be checked for
if ( == "Task"){
// Add Label code here

I hope this helps


Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Apps & Integrations

Send an Email or Publish to Confluence - What should you do with your release notes?

Background When you hear the words ‘Release notes’, almost always you think of an unsolicited email from a software vendor. But I am here to tell you that from our data, sending release notes via E...

246 views 1 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you