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 subtasks based on updated custom field value

Edited

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

 

 

 

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 {it.name == "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
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

552 views 15 18
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