Scriptrunner. How to enter some text into the field "Labels"?

Leo Epstein February 14, 2022

Hello,

Which code can I use to enter a text string XYZ  into the field "Labels"?

Many thanks in advance.

Regards

Leo

1 answer

1 accepted

2 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2022

Hi @Leo Epstein ,

please try the following code :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.label.Label;
import com.atlassian.jira.issue.label.LabelManager;
import com.atlassian.jira.security.JiraAuthenticationContext;

LabelManager labelManager = ComponentAccessor.getComponentOfType(LabelManager.class);
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
Label label = labelManager.addLabel(jiraAuthenticationContext.getLoggedInUser(), issue.getId(), "YOUR_LABEL_HERE", false);

Please let me know if it works.

Fabio

Leo Epstein February 14, 2022

Hi @Fabio Racobaldo _Herzum_ ,

It works, thank you very much!!!

This is exactly what I was looking for!

Just one more question:

Is it possible to make the script so, that it works even while creating a new issue in Jira?

If I try, I get the following message.

Regards, Leo

 


2022-02-14 16:03:26,786 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'leo'. View here: https://jira-XXX/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=XXX&descriptorTab=postfunctions&workflowTransition=1&highlight=1 com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issueId should not be null!

at com.atlassian.jira.util.dbc.Assertions.notNull(Assertions.java:25) at com.atlassian.jira.issue.label.DefaultLabelManager.addLabel(DefaultLabelManager.java:90) at com.atlassian.jira.issue.label.LabelManager$addLabel.call(Unknown Source) at com.atlassian.jira.issue.label.LabelManager$addLabel.call(Unknown Source) at Script764.run(Script764.groovy:9)

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2022

Hi @Leo Epstein ,

try the following scriptedpost function

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.label.Label;
import com.atlassian.jira.issue.label.LabelManager;
import com.atlassian.jira.security.JiraAuthenticationContext;

Issue currentIssue = issue;

LabelManager labelManager = ComponentAccessor.getComponentOfType(LabelManager.class);
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
Label label = labelManager.addLabel(jiraAuthenticationContext.getLoggedInUser(), currentIssue.getId(), "YOUR_LABEL_HERE", false);
Like # people like this
Leo Epstein February 14, 2022

Hi  @Fabio Racobaldo _Herzum_ ,
Thanks again!

Unfortunately I'm getting the same error "issueId should not be null!".

It seems, that an issue should be created first, and Post Function in Create Transaction cannot set a value of a field... :(

Regards

Leo


2022-02-14 17:24:24,641 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'leo'. View here: https://jira-XXX/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=XXX&descriptorTab=postfunctions&workflowTransition=1&highlight=1 com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issueId should not be null! at com.atlassian.jira.util.dbc.Assertions.notNull(Assertions.java:25) at com.atlassian.jira.issue.label.DefaultLabelManager.addLabel(DefaultLabelManager.java:90) at com.atlassian.jira.issue.label.LabelManager$addLabel.call(Unknown Source) at Script773.run(Script773.groovy:11)

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2022

Hi @Leo Epstein ,

you should place your scripted post function after "Create the issue originally " that's the trick.

Fabio

Like Leo Epstein likes this
Leo Epstein February 14, 2022

@Fabio Racobaldo _Herzum_ 

Wow!

It works!!!

Thank you!!!!!

Regards, Leo

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2022

you're welcome @Leo Epstein 

Like Leo Epstein likes this

Suggest an answer

Log in or Sign up to answer