title: JIRA Groovy Script - Check on duplicate sub-tasks
body: <p>The code below creates 3 sub-tasks for each Version selected in <strong>SW Version</strong> custom field. This is placed on a workflow transition as a post function and works perfectly. However, if the workflow transition is revisited it creates duplicate subtasks for the already selected versions. </p>
  
  <p>Example: 
  SW Version field has 5 options: 
  1, 2, 3, 4, 5</p>
  
  <p>if a user selected 1 and 2 and proceeds with the transition, it will create 6 subtasks 3 for each version selected. (All good here)</p>
  
  <p>if the user updates the SW Version field to 1, 2 , 3, and 4 which already has 1 and 2 pre-selected, it will create 12 additional subtasks making the total 18 with 6 duplicates of 1 and 2. </p>
  
  <p>I wanted to put a check on these duplicates, so basic logic is if the subtask already exist go to the next one. Something like for each version selected, check if subtasks already exist and proceed. I tried doing it various ways and failed. Have commented out some codes below to get summary of the subtasks and compare with all existing subtasks summary but it doesn't work. </p>
  
  <p>In addition I tried doing this to no avail. </p>
  
  <pre><code>if(issue.getSubTaskObjects()*.summary.equals(summaryText)){
       log.info("Subtask already exists")
       return;
  </code></pre>
  
  <p>Any help would be greatly appreciated ! </p>
  
  <pre><code> import com.atlassian.jira.component.ComponentAccessor
      import com.atlassian.jira.issue.IssueManager
      import com.atlassian.jira.issue.Issue
      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
      import org.apache.log4j.Logger
      import org.apache.log4j.Level
  
      log.info("Processing: " + issue.key);
  
      CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
      IssueManager issueManager = ComponentAccessor.getIssueManager();
  
      def cfM119 = customFieldManager.getCustomFieldObjectByName("SW Version")
  
  
      log.info("cfM119: " + cfM119)
  
      def m119VersionArray = issue.getCustomFieldValue(cfM119) as String[]
  
  
      def reqAssignee = 'user1'
      def swAssignee = 'user2'
      def testAssignee = 'user3'
  
      //collecting subtask object
  
      //Collection allsubtasks = issue.getSubTaskObjects()
      //for(Issue allsubtask: allsubtasks) {
      //def subtaskSummary = allsubtask.getSummary() as String[]
      //log.info("Subtask Summary" + subtaskSummary)
  
      //if (subtaskSummary[]){
        //  log.info("Subtask already exists")}
      //else {
  
  
      m119VersionArray.each{ version -&gt;
              createSubTask("", version, "_Approved_REQ", reqAssignee)
              createSubTask("", version, "_Approved_SW", swAssignee)
              createSubTask("", version, "_Approved_TEST", testAssignee)
              }
  
      def createSubTask(String component, version, type, String assignee) {  
      def Long issueLinkType = new Long (10702)
      def Long sequence = new Long (1)
  
      //Issue issue
      def summaryText = component + version + " " + type 
      def issueManager = ComponentAccessor.issueManager
      def issueFactory = ComponentAccessor.issueFactory
      def subTaskManager = ComponentAccessor.subTaskManager
      def issueLinkManager = ComponentAccessor.issueLinkManager
      def userManager = ComponentAccessor.userManager
      def authenticationContext = ComponentAccessor.jiraAuthenticationContext
  
       if(issue.getSubTaskObjects()*.summary.equals(summaryText)){
           log.info("Subtask already exists")
           return;
       }
  
      // 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)
  }   
  </code></pre>