Automatic creation of subtasks
Asked by Nero Mars:

**Hello, folks!**   

**I need your help**   

**I created 2 mygroovy-scripts as post functions:
the first one creates a subtask (this happens during the transition to the next status);
the second - changes the status of the issue to the next one in order (this happens during the transition to the last status of the subtask) (yes, each subtask has its own workflow).
The problem is that when the transition to the last status of a subtask is made, a new subtask is not created, although the status of the issue changes.**   
**Also the button for switching to the next issue status is active, although the transition has already been completed.**   

**I need the next subtask to be created when switching to the last status of a subtask.**   

**Below I am attaching scripts and a screenshot in this post:**   
**1) First script (for issue workflow):**

*def constantManager = ComponentAccessor.getConstantsManager()*   
*def customFieldManager = ComponentAccessor.getCustomFieldManager()*   
*// def user = ComponentAccessor.getJiraAuthenticationContext().getUser()*   
*def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()*   
*def issueFactory = ComponentAccessor.getIssueFactory()*   
*def subTaskManager = ComponentAccessor.getSubTaskManager()*   
*def issueManager = ComponentAccessor.getIssueManager()*

*String IDsubTask = ""*

*Issue parentIssue = issue*

<br />

*MutableIssue newSubTask = issueFactory.getIssue()*   
*newSubTask.setAssigneeId(parentIssue.assigneeId)*   
*newSubTask.setReporterId(parentIssue.reporterId)*   
*newSubTask.setDescription(parentIssue.getDescription())*   

*if (parentIssue.getStatusId() == '10600')*   
*{*   
*newSubTask.setSummary("Subtask - Backlog")*   
*IDsubTask = "10601"*   
*}*   
*else if (parentIssue.getStatusId() == '11400')*   
*{*   
*newSubTask.setSummary("Subtask - Analysis")*   
*IDsubTask = "10602"*   
*}*   
*else if (parentIssue.getStatusId() == '11401')*   
*{*   
*newSubTask.setSummary("Subtask - Development")*   
*IDsubTask = "10604"*   
*}*   
*else if (parentIssue.getStatusId() == '11402')*   
*{*   
*newSubTask.setSummary("Subtask - Testing")*   
*IDsubTask = "10605"*   
*}*   

*if (IDsubTask != "")*   
*{*   
*newSubTask.setParentObject(parentIssue)*   
*newSubTask.setProjectObject(parentIssue.getProjectObject())*   
*newSubTask.setIssueTypeId(IDsubTask)*   
*def newIssueParams = \["issue" : newSubTask\] as Map\<String,Object\>*

*issueManager.createIssueObject(user, newIssueParams)*   
*subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)*   
*}*

**2) Second script (for subtask workflows):**

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

*String IDsubTask = ""*   
*Issue parentIssue = issue.getParentObject()*   
*MutableIssue parentIssueM = (MutableIssue)parentIssue*

<br />

*if (issue.getIssueTypeId() == '10601')*   
*{IDsubTask = "11400"}*   
*else if (issue.getIssueTypeId() == '10602')*   
*{IDsubTask = "11401"}*   
*else if (issue.getIssueTypeId() == '10604')*   
*{IDsubTask = "11402"}*   
*else if (issue.getIssueTypeId() == '10605')*   
*{IDsubTask = "10000"}*

*if (IDsubTask != "")*   
*{*   
*parentIssueM.setStatusId(IDsubTask)*   
*issueManager.updateIssue(user,parentIssueM,EventDispatchOption.ISSUE_UPDATED,false)*   
*parentIssueM.store()*   
*}*   
*else{*   
*return*   
*}* **Screenshot:
![shot.PNG](/t5/image/serverpage/image-id/122780iDE2BBB0F6E33A9A2/image-size/large?v=v2&px=999 "shot.PNG")**


Answers:
author: Adam MacDonald
body: Hi Nero,

This doesn't answer your question . . but may be another way to achieve the same outcome.

Have you looked at project automation? This can do very similar things to what I think you are trying to achieve:

* Create Subtask
* Edit Subtask
* Transition Subtask
* etc

One of the problems with automatic subtask creation (in my experience anyways) is that even if you auto create the subtask using groovy or script runner or project automation, for some reason, the subtask list doesn't refresh automatically .. instead, you have to force a browser refresh before you can see the newly created sub tasks .. even though you can see the automation stuff kicking off calls.

Anyway . .. in project automation, here is an example of creating a sub task automatically based on some event, in this case, a transition:

![tmp.png](https://community.atlassian.com/t5/image/serverpage/image-id/122844i7FEE5CF2F5937C88/image-size/large?v=v2&px=999 "tmp.png")


author: Nic Brough -Adaptavist-
body: Everything in your second script looks wrong to me. I think you are saying that it is executed as a post-function in the sub-task process, and if that is trure, then you have two problems:

* You cannot just jam random data into a status. Status is not a field, it is an indicator of where something is in the workflow, and just setting it is nonsense, all it does it lie about where the issue is in the process, and break the process so you can't use the workflow any more. You need to ask Jira to transition the issue through the workflow.
* You cannot execute a transition on the current issue inside another transition, it either does nothing or trashes the transition, breaking the issue completely.

You will need to think again about when you want to transition the sub-task and then how you ask Jira to do the transition.

comments: author: Nero Mars
body: Hello, Nic!  

Did I understand correctly: when the post-function in the child workflow (workflow of sub-task) is triggered, which changes the status of the issue, then there is no transition in the main workflow (workflow of issue)?  

I just don't know how to change the status of issue in a post function that is attached to a subtask workflow.


author: Nic Brough -Adaptavist-
body: Partly.

Please don't say "child workflow", this confuses things, as there is no such thing as a "child workflow". There are simply workflows, which issues all follow.

The point here is that you cannot transition an issue during a transition that is already happening to it.

Imagine you have a really simple pair of issues - ABC-123 and ABC-567. ABC-567 is a sub-task of ABC-123.

* In the workflow of ABC-123, you can tell ABC-567 to go through a transition.
* In the workflow of ABC-567, you can tell ABC-123 to go through a transition.
* In the workflow of ABC-123, you can **not** tell ABC-123 to go through a transition.
* In the workflow of ABC-567, you can **not** tell ABC-567 to go through a transition.

Note that if you are doing "transition other" and the other also does "transition other", you need to be very careful not to fall into a recursive transition loop - if ABC-123 triggers a transition in ABC-567, then ABC-567 must **not** trigger a transition on ABC-123...


Tags: cloud

jira