Forums

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

Scriptrunner Listener: How to avoid creating duplicates using the built-in script Create a sub-task?

Diana
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.
July 10, 2023

I'm using the built-in custom listener script 'Create a sub-task' and I follow the guidance here

I have set the trigger events to Issue Created and Issue Updated. And whenever the parent issue is either created or updated with a custom field value = xyz, it will create the subtasks. Works like charm.

But now, every time the parent issue is updated on any field, it will create more sub-tasks.

I cannot share a screenshot but my code looks like this

Under Conditions:

def cf = customFieldManager.getCustomFieldObjectsByName("Radio Button")
def cfValue = cf.getAt(0).getValue(issue)

if (issue.issueType.name == "Bug" &&
cfValue.toString().equals("XYZ") &&
issue.getSubtaskObjects().find {it.summary() != "Custom summary here"}){

return true
}

Target issue set to 'sub-task',

copy all fields,

under Additional issue actions:

issue.summary = 'Custom summary here'

I've also tried using 'Subtask Action' and set it to 'Done', but the script still auto creates more subtasks as "To Do".

What do I need to add if I want the built-in script to search via the summary, and if that subtask summary already exist, then it will not create? 

1 answer

1 accepted

0 votes
Answer accepted
Evgenii
Community Champion
July 10, 2023

Hi, @Diana Gorv 

Try this condition

def cf = customFieldManager.getCustomFieldObjectsByName("Radio Button")
def cfValue = cf.getAt(0).getValue(issue)

if (issue.getSubtaskObjects().find { it.summary() != "Custom summary here" }) {
if (issue.issueType.name == "Bug" && cfValue.toString().equals("XYZ")) {
return true
}
} else {
return false
}
Diana
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.
July 10, 2023

@Evgenii Thanks for the quick response

I tried your code, but it still keeps duplicating.

I added a log.warn to debug, and it looks like it's showing there are null subtasks in the parent issue

def cf = customFieldManager.getCustomFieldObjectsByName("Radio Button")

def cfValue = cf.getAt(0).getValue(issue)

if (issue.getSubtaskObjects().find { it.summary() != "Custom summary here" }) {

   log.warn("Existing subtasks: " + issue.getSubtaskObjects().find {it.key} + " - " + issue.getSubtaskObjects().find {it.summary})

   if (issue.issueType.name == "Bug" && cfValue.toString().equals("XYZ")) {

      return true

   }

} else {

   return false

}
Do you know maybe if there is another way to check for existing subtasks?
Evgenii
Community Champion
July 10, 2023

Noticed one error. it.summary (or it.getSummary()), not it.summary()

Checked similar condition, it's working, no duplicates

image.png

Diana
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.
July 10, 2023

@Evgenii I made my fixes and still no luck.

I noticed that my logs is not showing the subtasks summary. It still points as null. I ran this script below on the Console to double check, and it.summary keeps return the issue keys and not the actual issue summary as a string

Ran this under Console to troubleshoot:

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.component.ComponentAccessor

def im = ComponentAccessor.getIssueManager()

def issue = im.getIssueObject("ABC-123") //this is the parent issue key

Collection subtasks = issue.getSubtaskObjects()

subtasks.each {

   it.summary

}
Do I have to manual reindex Jira in order for the subtask summary to be as a string and not as its issue key?
EDIT: I had tried putting in a reindex portion to the built-in script, and still it creates duplicates, and logs keeps showing subtasks are null
EDIT 2: I figured it out!
i end up using
issue.getSubtaskObjects()*.summary.contains('Custom summary here')
And now no duplicates are made!
But now I'm curious why my logs won't return the summary as a string.
Evgenii
Community Champion
July 11, 2023

To see summary in console, in .each loop you'll have to use log.warn

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor

def
im = ComponentAccessor.getIssueManager()
def issue = im.getIssueObject("ABC-123") //this is the parent issue key

Collection subtasks = issue.getSubtaskObjects()

subtasks.each {
   log.warn it.summary
}
Like Diana likes this

Suggest an answer

Log in or Sign up to answer