Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Automatic subtask creation

Marat May 28, 2019

Hi

I need to create several subtasks when creating a issue.
The number of subtasks depends on the filled in custom fields when creating a issue.
The user in the custom field must become a performer in subtasks

I saw in the scriptranner the possibility of creating subtasks, but it is difficult for me to figure out how to do what I described above.

pls Help)

2 answers

1 vote
Jack Brickey
Community Champion
May 28, 2019

You certainly will want an addon. Scriptrunner, Powerscripts, Automation for Jira all are good. I currently use the last one in this list and it is pretty easy to use. It is less "scripty". Bob Swift is coming out with one that focuses on "drag and drop" automation so that may be worth a look as well. It is best to assess you needs now and in the future, read the marketplace overviews and ultimately trial a few.

0 votes
Antoine Berry
Community Champion
May 28, 2019

To add to what Jack said, if you choose scriptrunner or already have it, here is a snippet that creates subtasks. You would have to create a custom script postfunction. 

import com.atlassian.jira.component.ComponentAccessor

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

def numberOfSubtasksId = 11001
def numberOfSubtasks = customFieldManager.getCustomFieldObject(numberOfSubtasksId)
def numberOfSubtasksValue = issue.getCustomFieldValue(numberOfSubtasks)

def performerId = 11002
def performer = customFieldManager.getCustomFieldObject(performerId)
def performerValue = issue.getCustomFieldValue(performer)

for (int i=1; i<=numberOfSubtasksValue; i++){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
newSubTask.setAssignee(performerValue?.getKey())
newSubTask.setSummary("Subtask n°" + i)
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for Subtask n°" + i)
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}

Assuming numberOfSubtasks is a Number field and performer is a User picker (single).

Antoine

Marat May 28, 2019

Yes, we already have scriptrunner.
I have several custom fields "Person 1 - n "(User picker (single)) When a issue is created, it is necessary that subtasks are created depending on the filled "Person" fields and each Person becomes an executor in a subtask.

Antoine Berry
Community Champion
May 28, 2019

How do you know how many subtasks you want to create ?

Marat May 28, 2019

That is the problem.
if three fields were filled
Person1
Person2
Person3
then there should be three subtasks
for Person 1-3.

Antoine Berry
Community Champion
May 28, 2019

Then you can use this (10001,10002,10003 being the custom field ids).

import com.atlassian.jira.component.ComponentAccessor

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

def personFieldsIds = [10001,10002,10003]

for (int personFieldsId:personFieldsIds){
def person = customFieldManager.getCustomFieldObject(personFieldsId)
def personValue = issue.getCustomFieldValue(person)
if (personValue){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
newSubTask.setAssignee(personValue?.getKey())
newSubTask.setSummary("Subtask for " + personValue?.getDisplayName())
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for " + personValue?.getDisplayName())
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}
}

 Also you could use a multi user field instead of three single ones.

Antoine

Marat May 28, 2019

This script does not work for me. The validator accepted it, but subtasks are not created. What am I doing wrong ? Maybe affairs in the Jira version? My version v6.3.10

Antoine Berry
Community Champion
May 28, 2019

That might be because of the version indeed, I am working on 7.6. Could you please take a look into the logs and paste the error here ?

Antoine

Marat May 28, 2019

in logs

/secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script195.groovy: 14: unable to resolve class MutableIssue
@ line 14, column 14.
MutableIssue newSubTask = issueFactory.getIssue()
^
1 error

Antoine Berry
Community Champion
May 29, 2019

Please add 

import com.atlassian.jira.issue.MutableIssue

 

Marat May 29, 2019

new error

[workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script8
 at Script8.run(Script8.groovy:13)
Marat May 30, 2019

@Antoine Berry  any idea?

Antoine Berry
Community Champion
May 31, 2019

Hi @Marat , sorry I was out of office.

Please add : 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

Antoine

Marat May 31, 2019

Thank you for continuing to answer me))

That didn't help either55.PNG

Antoine Berry
Community Champion
May 31, 2019

Please save as is, these warnings appear beacuse we did not specify the class of the object, but it should not be a problem at execution time.

Antoine

Marat May 31, 2019

new error


2019-05-31 13:06:35,116 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-05-31 13:06:35,116 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (java.lang.String) values: [testuser] Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId() at Script158.run(Script158.groovy:18)
Antoine Berry
Community Champion
May 31, 2019

Is your field a single user picker ? 

Can you add 

log.error("personValue : " + personValue)
log.error("personValue class : " + personValue.getClass())

before the setAssignee line and paste the logs again ?

Marat May 31, 2019
2019-05-31 13:43:54,357 ERROR [workflow.ScriptWorkflowFunction]: personValue : TESTUSER(testuser)
2019-05-31 13:43:54,357 ERROR [workflow.ScriptWorkflowFunction]: personValue class : class com.atlassian.jira.user.DelegatingApplicationUser
2019-05-31 13:43:54,360 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-05-31 13:43:54,360 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (java.lang.String) values: [testuser]
Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId()
 at Script176.run(Script176.groovy:20)
Antoine Berry
Community Champion
May 31, 2019

Please replace setAssignee line with : 

newSubTask.setAssignee(personValue)

 or 

newSubTask.setAssigneeId(personValue?.getKey())

Antoine

Marat May 31, 2019
2019-05-31 14:04:45,525 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-05-31 14:04:45,525 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Parent issue cannot have a null ID.
 at com.atlassian.jira.issue.IssueImpl.setParentObject(IssueImpl.java:1087)
 at com.atlassian.jira.issue.MutableIssue$setParentObject$6.call(Unknown Source)
 at Script184.run(Script184.groovy:22)

2019-05-31 13:53:53,608 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-05-31 13:53:53,608 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser) values: [testuser(testuser)]
Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId()
 at Script180.run(Script180.groovy:20)
Antoine Berry
Community Champion
May 31, 2019

So first option seems to work for Assignee, the error is not the same. Where are you calling this script ? It seems that issue object is null.

Marat May 31, 2019

The script is written in the post function when creating a issue.

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

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

def personFieldsIds = [14098,14099]

for (int personFieldsId:personFieldsIds){
def person = customFieldManager.getCustomFieldObject(personFieldsId)
def personValue = issue.getCustomFieldValue(person)
if (personValue){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
log.error("personValue : " + personValue)
log.error("personValue class : " + personValue.getClass())
newSubTask.setAssigneeId(personValue?.getKey())
newSubTask.setSummary("Subtask for " + personValue?.getDisplayName())
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for " + personValue?.getDisplayName())
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}
}



ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Parent issue cannot have a null ID.
 at com.atlassian.jira.issue.IssueImpl.setParentObject(IssueImpl.java:1087)
 at com.atlassian.jira.issue.MutableIssue$setParentObject$6.call(Unknown Source)
 at Script184.run(Script184.groovy:22)
Antoine Berry
Community Champion
May 31, 2019

Can you please provide a screenshot of your transition post functions ? The script post function should be after the "Creates the issue originally.".

Antoine

Marat May 31, 2019

Moved script post function after the "Creates the issue originally." and got a new error))

2019-05-31 16:59:56,316 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: UWORK-43, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot get property 'id' on null object
 at Script184.run(Script184.groovy:24)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events