Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Listeners - Delete sub-tasks based on updated custom field value

Hi,

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

3 answers

1 accepted

The below script will delete all the subtasks associated with the issue that fired the listener event.

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

def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// Get a list of the current issue's subtasks
def subtasks = issue.getSubTaskObjects()

// Loop through all the subtasks and delete them
subtasks.each {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)
}

Hi @Roland Holban _Adaptavist_

How would i change the code to only delete subtasks that begin with the word "Test" instead of deleting all subtasks?

E.g. I have subtasks such as Test Design, Test Execution, and i only want those subtasks to be deleted.

Inside the loop, you could add an if statement that checks whether the first 4 letters of the subtask's summary is "Test"

subtasks.each {
if (it.summary.substring(0,4) == "Test") {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)
}
}

The script below will delete all the subtasks associated with the issue that fired the listener event.

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

def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// Get a list of the current issue's subtasks
def subtasks = issue.getSubTaskObjects()

// Loop through all the subtasks and delete them
subtasks.each {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)
}

This script will delete all the subtasks associated with the issue that fired the event.

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

def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

// Get a list of the current issue's subtasks
def subtasks = issue.getSubTaskObjects()

// Loop through all the subtasks and delete them
subtasks.each {
issueManager.deleteIssue(currentUser, it, EventDispatchOption.ISSUE_DELETED, false)
}

The last portion of the code shouldn't all be commented out. The Atlassian Community code block feature is really bad. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

51 views 0 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you