Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Hi,

 

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(issue.id).collect{it.getLabel()}
labels += 'Incident'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

 

 

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?

Thanks,

Kasia

3 answers

Solved:

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

def field = getFieldById("customfield_12021");
field.convertToMultiSelect();
field.setFieldOptions(option);
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 = [
(newValue1.toInteger()):newValue1,
(newValue2.toInteger()):newValue2,
];

def field = getFieldById("customfield_12021");
field.convertToMultiSelect();
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

Hi,

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 (issue.issueTypeObject.name == "Task"){
// Add Label code here
}

I hope this helps

Kristian

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(issue.id)*.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, issue.id, labels.toSet(), false, false)

 



Hi, @Matthew Beda  is not working.

at Script1.run(Script1.groovy:17)
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 100.21.105.10,0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json

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

contextIssue does not have an identifier

What version of Jira are you using?

Suggest an answer

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

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

340 views 0 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