Scriptrunner Create Subtask for a task which is created by "Clones an issue, and links"

Nazimuddin Mohammed September 7, 2018

Created a Task in a different project by using "Clones an issue, and links" with the following Additional issue actions.

import com.onresolve.scriptrunner.runner.util.UserMessageUtil;
import com.atlassian.jira.component.ComponentAccessor;

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Name'}
issue.setCustomFieldValue(cf, 'Cloned Issue Epic Name' + sourceIssue.key + ': ' + sourceIssue.summary)

issue.summary = 'Cloned Issue ' + sourceIssue.key + ': ' + sourceIssue.summary

 The issue is created successfully and also linked with Issue link type "relates to".

Once this new issue is cloned and created, I would three subtasks to be created for the newly created issue by adding some actions to the existing script. 

How can this be achieved?

2 answers

1 accepted

0 votes
Answer accepted
JohnsonHoward
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.
September 11, 2018

Hi Nazimuddin,

You can't add the subtask in the same script as technically the new issue won't have been created yet. So passing the .setParentId() method the new issue id won't work as it does not exist until the script finishes. 

Therefore the only way to do this would be to add another postfunction directly after that identifies the newly created issue and adds subtasks to it.

I have done something similar before and this is the code I used: 


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink;

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

def summariesList = ["Summary 1", "Summary 2", "Summary 3"]
Issue issue = issue
def newIssue = getNewIssue(issue, "is cloned by", issue.summary)
if(!newIssue) return null
summariesList.each {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(newIssue.assigneeId)
newSubTask.setSummary(it)
newSubTask.setParentObject(newIssue)
newSubTask.setProjectObject(newIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{ it.getName() == "Sub-task"}.id)
// Add any other fields you want for the newly created sub task
def newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(newIssue, newSubTask, user)
}


public static def getNewIssue(Issue issue, def linkTypeName, def mainIssueSummary) {
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
Collection<IssueLink> issueLinks = issueLinkManager.getInwardLinks(issue.id).findAll { issueLink ->
issueLink.issueLinkType.inward == linkTypeName
}
def newIssue = issueLinks.find{it.destinationObject.summary == mainIssueSummary}.sourceObject

return newIssue

Hopefully this will help you out.

Let me know if you have any issues.

Johnson Howard [Adaptavist] 

Nazimuddin Mohammed September 14, 2018

Hi Johnson,

Thank you for the code. I needed to create three stories in and link them to the cloned Epic. I used your source code and just it a bit by creating story instead or subtask and it worked. Thank you.

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink;
import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;

//Enable logging
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

//Local Defines of ComponentAccessor members
def constantManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

Issue issue = issue

//Finding the linked issue based on the name of the summary

Collection<IssueLink> issueLinks = issueLinkManager.getOutwardLinks(issue.id).findAll { issueLink ->
issueLink.issueLinkType.outward == "relates to"
}

def newIssueSummary = 'Cloned Issue ' + issue.key + ': ' + issue.summary
def newIssue = issueLinks.find{it.destinationObject.summary == newIssueSummary}.destinationObject


//Creating three stories for the Epic newIssue

def summariesList = ["Summary 1", "Summary 2", "Summary 3"];

summariesList.each {
MutableIssue newStory = issueFactory.getIssue();
newStory.setProjectId(newIssue.projectId);
newStory.setSummary(it);
newStory.setReporterId(newIssue.reporterId)
newStory.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Story"}.id)

def newIssueParams = ["issue" : newStory] as Map<String, Object>
issueManager.createIssueObject(user, newIssueParams)

def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
epicLink.updateValue(null, newStory, new ModifiedValue(issue.getCustomFieldValue(epicLink), newIssue),new DefaultIssueChangeHolder())
}
2 votes
Elifcan Cakmak
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.
September 11, 2018

Hello

You can add subtask to an issue with this script 

import com.atlassian.jira.security.JiraAuthenticationContext;

def user = authenticationContext.getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()

//create new issue as sub task
def newSubtask = issueFactory.getIssue()
newSubtask.summary = "new subtask"
newSubtask.description = "subtask explanation"
newSubtask.setProjectId(issue.getProjectId())
newSubtask.setIssueTypeId("10003") //issue type id must be subtask type's id
newSubtask.setParentId(issue.getId())
newSubtask.setAssigneeId("user1")
newSubtask.setReporterId("user2")
newSubtask = issueManager.createIssueObject(user, newSubtask)

//link subtask to parent task
def subTaskManager = ComponentAccessor.getSubTaskManager()
subTaskManager.createSubTaskIssueLink(issue, newSubtask, user)

issue.store()

You can customize it to add 3 subtasks with a for loop. I think it should work when you add this code to your code. If it doesn't, you can add it as a separate script postfunction to the create transition of clone project and it should work.

Regards

Nazimuddin Mohammed September 14, 2018

Thank you for the input Elifcan.

Suggest an answer

Log in or Sign up to answer