Script Runner: Create sub-task with specific label

Hi,

 

I use Script Runner's postfunction to create some sub-tasks during a transition. Is there a way to set new individual labels to a few of the sub-tasks? I tried "issue.label = somelabel;" at the additional issue actions, but it won't work.

 

Regards

Ja

1 answer

1 accepted

1 vote
Vasiliy Zverev Community Champion Jun 27, 2016

You should use LabelManager.addLabel() to add labels like this:

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
labelManager.addLabel(ComponentAccessor.getJiraAuthenticationContext().getUser(), issue.getId(), "label", false)

I use JIRA 7.1.4 and Script Runner 4.3.1 so i had to change it to

LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
labelManager.addLabel(ComponentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue.getId(), "Label", false)

But it still doesn't work, my log says

/secure/WorkflowUIDispatcher.jspa [c.o.s.jira.wo
rkflow.ScriptWorkflowFunction] Script function failed on issue: DEMO-525, actionId: 221, file: null
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:104)
        at com.atlassian.jira.issue.label.LabelManager$addLabel$0.call(Unknown Source)
        at Script1382.run(Script1382.groovy:10)

Is this on the issue create transition? It looks like issue.getId() is null when you pass it to the add label method.

Vasiliy Zverev Community Champion Jun 28, 2016

You should place postfunction after issue creation function.

I use the transition between open and in progress. In this transition there is only the create sub-task postfunction, so I can't move it around.

Vasiliy Zverev Community Champion Jun 28, 2016

When subtask is created you can get created issue into CreateResult variable. Use it to get issue id. Could you provide code to create a subtask?

the problem is the "additional code" section runs before the issue is actually created.

You could try putting Vasily's code in a "doAfterCreate" closure, eg:

doAfterCreate {
  // add label
}

it may work.

sadly it doesn't work. Seems to me that there is no option for this.

Hi Ja,

Combining the feedback you got from the guys above, try 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
doAfterCreate = {
	def labelManager = ComponentAccessor.getComponent(LabelManager)
	labelManager.addLabel(ComponentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue.getId(), "Label", false)
}

If it still doesn't work please attach the error you get

regards

Thanos

 

Hi Thanos,

 

Thank you very much! Now it works fine. 

 

Regards

Jan

Cool. The doAfterCreate closure is not documented yet but it will be in the next documentation update (which will be soon)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

56 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you