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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,033
Community Members
 
Community Events
176
Community Groups

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

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

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

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)

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

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)

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

@Fabio Racobaldo _Herzum_ 

Wow!

It works!!!

Thank you!!!!!

Regards, Leo

you're welcome @Leo Epstein 

Like Leo Epstein likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events