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

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.

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.

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Fadoua M. Boualem
Published Monday in Trello

Using Trello to manage events

As a Jira power user, I was at first doubtful that Trello could benefit my workflow. Jira already uses boards (ones you can customize!), so why would I even need to use Trello?! In this post you will...

513 views 8 9
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