Script Runner: Create sub-task with specific label

JaA June 27, 2016

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
Answer accepted
Vasiliy Zverev
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.
June 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)
JaA June 27, 2016

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)
adammarkham
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.
June 28, 2016

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
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.
June 28, 2016

You should place postfunction after issue creation function.

JaA June 28, 2016

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
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.
June 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?

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.
June 29, 2016

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.

JaA July 1, 2016

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

Thanos Batagiannis _Adaptavist_
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.
August 17, 2016

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

 

JaA August 25, 2016

Hi Thanos,

 

Thank you very much! Now it works fine. 

 

Regards

Jan

Thanos Batagiannis _Adaptavist_
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.
August 25, 2016

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