I have already created the following script that works for creating sub-tasks based on Version selections. It creates 3 sub-tasks for each version selected during the transition.
I need to check for a condition if sub-tasks already exist, then don't create it. Currently if I go back on the workflow and re-run the transition, it creates duplicate sub-tasks.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.index.IssueIndexingService
log.info("Processing: " + issue.key);
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
IssueManager issueManager = ComponentAccessor.getIssueManager();
def cfM119 = customFieldManager.getCustomFieldObjectByName("SW Version To Be Fixed In")
log.info("cfM119: " + cfM119)
def m119VersionArray = issue.getCustomFieldValue(cfM119) as String[]
def reqAssignee = 'xxx'
def swAssignee = 'xxx.'
def testAssignee = 'xxx'
m119VersionArray.each{ version ->
createSubTask("", version, "_Approved_REQ", reqAssignee)
createSubTask("", version, "_Approved_SW", swAssignee)
createSubTask("", version, "_Approved_TEST", testAssignee)
}
def createSubTask(String component, version, type, String assignee) {
def summaryText = component + version + " " + type
def Long issueLinkType = new Long (10702)
def Long sequence = new Long (1)
// Issue issue
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def subTaskManager = ComponentAccessor.subTaskManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def userManager = ComponentAccessor.userManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
// Defining subtask
def newIssue = issueFactory.getIssue()
newIssue.setIssueTypeId("5")
newIssue.setParentId(issue.getId())
newIssue.setProjectObject(issue.getProjectObject())
newIssue.setSummary(summaryText)
newIssue.setAssignee(userManager.getUserByName(assignee))
newIssue.setDescription(issue.getDescription())
log.info("Creating subtask - " + summaryText)
def subTask = issueManager.createIssueObject(authenticationContext.getLoggedInUser(), newIssue)
subTaskManager.createSubTaskIssueLink(issue, subTask, authenticationContext.getLoggedInUser())
issueLinkManager.createIssueLink(issue.getId(), newIssue.getId(), issueLinkType, sequence, authenticationContext.getLoggedInUser())
// reindex
ImportUtils.setIndexIssues(true)
IssueIndexingService issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
issueIndexService.reIndex(subTask)
ImportUtils.setIndexIssues(false)
}
How would you know if sub task exists or not?
You have to make assumptions based on your business logic.
For example (and this is a real one I wrote listeners for, although I've simplified)
If the issue is edited, the attendees field has Laura added. So, my code has to go:
In this case, the logic is quite simple, and the users know that if they edit a summary of a sub-task, they may end up with a duplicate. We're not yet enforcing a stronger check in code, just monitoring it to see if it does become a problem.
I think @SidhR needs to look at his business logic, and work out how to answer the question "does this sub-task already exist" in that context.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the clarification Nic. This is what I have managed so far but still having hard time what to compare the Summary with?
def reqAssignee = 'xxx'
def swAssignee = 'xxx.'
def testAssignee = 'xxx'
Collection allsubtasks = issue.getSubTaskObjects()
for(Issue allsubtask: allsubtasks) {
def subtaskSummary = allsubtask.getSummary()
log.info("Subtask Summary" + subtaskSummary)
if (subtaskSummary == (compare with ?){
log.info("Subtask already exists")}
else {
m119VersionArray.each{ version ->
createSubTask("", version, "_Approved_REQ", reqAssignee)
createSubTask("", version, "_Approved_SW", swAssignee)
createSubTask("", version, "_Approved_TEST", testAssignee) }
}
}d
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.