Hi,
I need to know how to create muliplte subtasks as a postfunction through a workflow transition.
I have to create upwards of 20+ subtasks per workflow status and need to know how they can be automatically added through transitions.
Does anyone have any script that will be of assistance.
//This is a variation of Joshua Yamdogo's script above. I actually use two in the same //transition, since the value of the custom field determines additional subtasks
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
log.setLevel(Level.DEBUG)
def cfManager = ComponentAccessor.customFieldManager
def stList = []
//Validate the value in custom field to determine if the additional subtasks need to be created
final cfTypeName = 'cf Field Name'
def cfType = cfManager.getCustomFieldObjects(issue).find {it.name == cfTypeName }
assert cfType : "Could not find this field on this issue $cfTypeName"
//Declare utility managers
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
//Capture the parent key value (should not be an issue inside a post function)
final parentIssueKey = issue.getKey()
def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(parentIssueKey)
assert parentIssue : "Could not find parent issue with key $parentIssueKey"
//Set the subtask item type to be created (5='Sub-task')
final issueTypeId = '5'
//Current user is the Reporter on the subtasks
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
final reporterKey = user
//Assignee is the person assigned within the transition
final userKey = issue.getAssigneeId()
//Priority is inherited from parent issue
final priorityName = issue.getPriority()
//Create an array of subtask objects
stList = [
[summary:'Summary 1',description:"Description 1",originalEstimate:1800,estimate:1800],
[summary:'Summary 2',description:"Description 2",originalEstimate:1800,estimate:1800],
[summary:'Summary 3',description:"Description 3",originalEstimate:900,estimate:900]
]
stList.each { it ->
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(userKey)
newSubTask.setSummary(it.get('summary').toString())
newSubTask.setDescription(it.get('description').toString())
newSubTask.setOriginalEstimate(it.get('originalEstimate').toLong())
newSubTask.setEstimate(it.get('estimate').toLong())
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(issueTypeId)
// Add any other fields you want for the newly created sub task
newSubTask.setPriority(parentIssue.priority)
newSubTask.setReporter(reporterKey)
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
log.info "New item with summary ${newSubTask.summary} created"
}
Hi Rhian,
Do you need each of these sub-tasks to have different information? Or will all of the sub-tasks contain all of the same information? Creating sub-tasks automatically on a workflow function is already a feature built-in to ScriptRunner.
This built-in feature would work well if you need each sub-task to have different information, since you could customize each sub-task individually as a post-function. However, if you just need to create 20+ sub-tasks with the same information, you'd probably be better off with a post-function custom script like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
def constantManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
Issue parentIssue = issue
if (parentIssue.getIssueTypeObject().getName() == 'Sub-task')
return
if (parentIssue.getIssueTypeObject().name != 'Feature')
return
def summariesList = ["Summary 1", "Summary 2", "Summary 3"]
summariesList.each {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setSummary(it)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
// Add any other fields you want for the newly created sub task
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
//for JIRA v6.*
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
// for JIRA v7.*
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
log.info "Issue with summary ${newSubTask.summary} created"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for replying Joshua,
I need to have different subtasks at different stages of the transition however there will be many subtasks and I have been told that a custom script will be best for this.
I have tried the custom script you have provided however I am still getting a failed response:
unable to resolve class for:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
Struggling to get a working script at the moment haha.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.