Forums

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

Script runner: creating sub-tasks based on Version field (selections) - Don't create if already exis

SidhR March 26, 2018

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)
}

1 answer

0 votes
Alexey Matveev
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.
March 26, 2018

How would you know if sub task exists or not?

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.
March 27, 2018

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)

  • Event: Team Tour Amsterdam
  • Event has a multi-user select field "attendees"
  • Listener creates sub-tasks like this:
    • Sub-task: Hotel for Eliza
    • Sub-task: Hotel for Nic
    • Sub-task: Hotel for Neil

If the issue is edited, the attendees field has Laura added.  So, my code has to go:

  • For each attendee (Eliza, Laura, Neil, Nic)
    • Check if there is already a subtask with summary "Hotel for <attendee>"
    • If not, then create a new one for the new person (The code will only create "Hotel for Laura" when she's added to the field)

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.

SidhR March 30, 2018

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 

 

Suggest an answer

Log in or Sign up to answer