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

This widget could not be displayed.
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
Community showcase
Posted 5 hours ago in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

19 views 0 1
Join discussion

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