How do I add a label in post function using scriptrunner?

Nicole Murphy March 17, 2016

I tried using both scripts on https://answers.atlassian.com/questions/307827 but neither were successful. When adding the script, the error says "cannot find matching method...". Has anyone been successful with this? Thank you!

1 answer

1 accepted

2 votes
Answer accepted
Aleks Yenin (Polontech)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

"cannot find matching method..."

That's all about error or is there something else? Seems to be something more.

Try to debug both scripts in a script runner - works for me as well.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

The API may have changed since that answer. As Aleks said, the useful part of the error you get is in the "...". 

Nicole Murphy March 17, 2016
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
  
def user = ComponentAccessor.jiraAuthenticationContext.getUser()
  
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels += 'new_label'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

The error I get is:
"Static Type Checking -Cannot find matching method: com.atlassian.jira.issue.label.LabelManager#setlabels(com.atlassian.jira.user.Ap
 java.lang.Long, java.util.Set , boolean, boolean). Please check if the declared type is right and the method exists"

Like Yash Jain likes this
Nicole Murphy March 17, 2016

The exact script I'm getting this error on is below (the code block above didn't work for some reason):
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

def user = ComponentAccessor.jiraAuthenticationContext.getUser()

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
def labels = labelManager.getLabels(issue.id).collect{it.getLabel()}
labels += 'new_label'
labelManager.setLabels(user,issue.id,labels.toSet(),false,false)

Aleks Yenin (Polontech)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

Hm... Which JIRA version do you use? Maybe it is old enough and do not have this method

Nicole Murphy March 18, 2016

6.4.6

Aleks Yenin (Polontech)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 18, 2016

Yes, there is difference. In JIRA 7 method signature is 

setLabels(ApplicationUser remoteUser, Long issueId, Set<String> labels, boolean sendNotification, boolean causeChangeNotification)

but in 6.4.6

setLabels(com.atlassian.crowd.embedded.api.User remoteUser, Long issueId, Set<String> labels, boolean sendNotification, boolean causeChangeNotification)

Try to replace last string this way:

labelManager.setLabels(user.getDirectoryUser() ,issue.id,labels.toSet(),false,false)
Nicole Murphy March 18, 2016

that did the trick! Thank you so much!

Dan27 June 25, 2019

Hello,

This code creates a "labels" , not a specific custom field with label type..

How can I set value in a custom field (with label type)

 

Thanks,

Daniel

Pratik Rawlekar May 15, 2020

@Nicole The code snippet by you above does not add new labels in the LABEL table, instead it updates the existing record in LABEL table.

 

 

Any update from your side for same?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events