Forums

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

How to create dynamic number of subtask

Etam_DSI_DW_CRM June 18, 2021

Hello

Here my situation : I've got a custom number field (id 17002) who can be empty or with a value (between 1 and a lot, not define)

I want create the same number of substask than the value in the custom field, and no creation if the field is empty

I try with automation, but I don't want to create 1 case for each number (and I don't have a max value)

I try with scriptrunner. I succed to create 1 subtask. But :

- if value is empty, subtask still created

- didn't sucess to create X subtask (X = value in my custom field)

I'm on Jira server

Any help will be welcome

Lot of thanks

1 answer

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2021

With Scriptrunner, all you need to do is add an "if" into your code - "if number is not 0/empty, then create one or more subtasks".  Or if you're creating one per value, handle a zero as part of the loop ( "for each X, create a subtask" - when X is zero, there's no X to create for)

Etam_DSI_DW_CRM June 28, 2021

Hello again

I try to do this, but without success

Here the code I wrote

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.config.SubTaskManager

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_17002")
def cFieldValue = issue.getCustomFieldValue(cField)

if (cFieldValue != null){
return true

//create new issue as sub task
def newSubtask = issueFactory.getIssue();
newSubtask.summary = "truc"+i;
newSubtask.setIssueTypeId("12801"); // 12801 is the ID of the subtask I want create
newSubtask = issueManager.createIssueObject(user, newSubtask)
}
else {
return false}

 

No subtask are created

For information, I've selected the post function "Create a sub-task [ScriptRunner]" in my workflow

Thank for your help

Etam_DSI_DW_CRM June 28, 2021

Hello

Thank for your reply

Here what I wrote, but without success (any subtask are created)

For information, I've selected the Create a sub-task [ScriptRunner] postfunction

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.config.SubTaskManager

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_17002")
def cFieldValue = issue.getCustomFieldValue(cField)

if (cFieldValue != null){
//create new issue as sub task
def newSubtask = issueFactory.getIssue();
newSubtask.summary = "truc"+i;
newSubtask.setIssueTypeId("12801"); // 12801 is the ID of the subtask I want create
newSubtask = issueManager.createIssueObject(user, newSubtask);
}
else {
return false}

Before I will create a dynamic number of subtask, how create 1 task depending of the value of a custom field (17002 into my case) ? 

Thank for your help

Etam_DSI_DW_CRM June 28, 2021

Hello

Thank for your answer, but I will need more help

I've selected the Create a sub-task [ScriptRunner] post function on my workflow

Here the code I wrote

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.config.SubTaskManager

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_17002")
def cFieldValue = issue.getCustomFieldValue(cField)

if (cFieldValue != null){
//create new issue as sub task
def newSubtask = issueFactory.getIssue();
newSubtask.summary = "truc"+i;
newSubtask.setIssueTypeId("12801"); // 12801 is the ID of the subtask I want create
newSubtask = issueManager.createIssueObject(user, newSubtask);
}
else {
return false}

 But when I made the transition, any subtask are created (even if my custom field is not empty or null)

How can I create a subtask with this condition ?

And, after, how create X substack depending of this condition ?

Regards


Alex

Suggest an answer

Log in or Sign up to answer