It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Sign up to answer
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

3,150 views 11 5
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you