Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

ScriptRunner Listeners - Delete subtasks based on updated custom field value




I've created a listener to create subtasks based on a custom field radio button (Testing Required = Yes). using the built in 'create a subtask' listener. I would now like to create a listener for the opposite requirement of deleting subtasks when custom field radio button (Testing Required = No)

I'm very nooby to all of this so i've been trying to use the built in listeners as much as possible. Is this possible to do and if not is there already a solution out there?

Edit: I've found the following code, how could i edit this to do the job i want?


import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

// for JIRA v6.*
def user = ComponentAccessor.getJiraAuthenticationContext().user.directoryUser

// for JIRA v7.*// def user = ComponentAccessor.getJiraAuthenticationContext().userdef issueManager = ComponentAccessor.getIssueManager()

// will return a Collection<Issue> with all the subtasks of the current issue

def subTasksList = issue.getSubTaskObjects()

for (subTask in subTasksList) {    

// add a condition here if you DO NOT want to delete all the sub-tasks    

issueManager.deleteIssue(user, subTask, EventDispatchOption.ISSUE_DELETED, false)}




2 answers

1 accepted

2 votes
Answer accepted

You could try a custom script listener, listening for an Issue Updated event and the script can be something like 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = issue as MutableIssue

def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def radioButtonValue = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find { == "RadioButtons"}?.getValue(issue)?.value

if (radioButtonValue == "No" && !issue.isSubTask()) {
issue.getSubTaskObjects()?.each {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)

Regards, Thanos

Hi Thanos,


I'm having a couple of issues with the code above, as it produces the following errors:

2018-05-23 11_37_07-Script Listeners - JIRA.png2018-05-23 11_38_29-Script Listeners - JIRA.png

Is safe to ignore this error. Is because the issue variable is bound to the triggered event. The static type checking is not that clever :)

Give it a go ....

Hi @Thanos Batagiannis [Adaptavist],

I've been using your code mentioned above and it has been a great success, although i need to make 1 slight change. Instead of deleting ALL subtasks, how would i go about specifying only deleting subtasks that begin with the word 'Test *'

Hey Craig,

I suppose by begin with, you mean the summary. In that case, line 12 should be 

issue.getSubTaskObjects()?.findAll {it.summary.startsWith("Test ")}?.each {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)

Thanks Thanos thats perfect :)

Does anyone know a solution for this?

Hey Craig,

This should definitely be possible, especially given that you've already implemented the opposite scenario.

In theory all you should have to do, is on the condition where you check that the custom field radio is "Yes", use the same code, but change the condition check to "No".

If your happy to reply with your code for the first solution I'd be happy to show you how to make that change :-)

Thanks for your reply. For the first example i used a scriptrunner built-in listener to create a subtask based on the radio button being updated to yes. Although, there is no built-in listener for the delete.

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events