How to set Label for specific task using script?


I have a script that create two substaks and I'm trying to set a label on my "subtask1".

Before I have a different script that was setting the label, even with permission "Edit Issue" denied for normal users, but now that I created theses differents scripts it is not possible to set this label for subtask1.


def subTask1 = SubTask.createSubTask(issue, "Execute Maintenance Window", "SPB", "O&M task", dtMWDate, dtStartDate, dtMWDate, userOm)
SubTask.setLabel(subTask1, "MW")

def subTask2 = SubTask.createSubTask(issue, "Close RFC Ticket", "SPB", "O&M task", dtMWDate+14, dtStartDate, null, userOm)


static void setLabel(Issue issue, String strLabel) {
      Logger log = Logger.getInstance("tools.SubTask")

      def user                   = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    LabelService labelService    = ComponentAccessor.getComponent(LabelService)

    AddLabelValidationResult validationResult = labelService.validateAddLabel(user, issue.getId(), strLabel)
    if (!validationResult.errorCollection.hasAnyErrors()) {
        labelService.addLabel(user, validationResult, false) "Label '${strLabel}' set to issue ${issue}."
    } else {
        log.error ${validationResult.errorCollection}


2017-05-25 16:33:32,822 http-nio-8081-exec-24 ERROR 993x16314x1 kg958x /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: SDP-7765, actionId: 1, file: TaskCreationDispatcher.groovy
groovy.lang.MissingMethodException: No signature of method: static SubTask.$() is applicable for argument types: (SubTask$_setLabel_closure3) values: [SubTask$_setLabel_closure3@12ee538c]
Possible solutions: is(java.lang.Object), run(), run(), any(), any(groovy.lang.Closure), use([Ljava.lang.Object;)
	at SubTask.setLabel(SubTask.groovy:235)
	at SubTask$setLabel$ Source)
	at - Software Version Upgrade.groovy:45)

How can I do it?


1 answer

Hi Artur,

Assuming that you have a custom post function that creates two subtasks and this post function is the first one, or at least before the "Save Issue step". Then a script like the one below will create two subtasks and add the label_a and label_labels in each of your subtasks. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.label.LabelManager

def parentIssue = issue as MutableIssue
if (parentIssue.isSubTask())

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["summary1", "summary 2"]

def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

summariesList.each { subTaskSummary ->
    MutableIssue newSubTask = issueFactory.getIssue()
        it.getName() == "Sub-task"
    // Add any other fields you want for the newly created sub task

    Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
    def subtask = issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user) "Issue with summary ${newSubTask.summary} created"

    ComponentAccessor.getComponent(LabelManager).setLabels(user, subtask?.id, ["label_a", "label_b"].toSet(), false, false)

Please let me know if this does the trick.

regards, Thanos 

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

321 views 0 12
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot