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

ScriptRunner Listeners - Delete subtasks based on updated custom field value

craig rayner May 22, 2018

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

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
May 23, 2018

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

craig rayner May 23, 2018

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

Thanos Batagiannis [Adaptavist]
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.
May 23, 2018

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 ....

craig rayner June 5, 2018

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 *'

Thanos Batagiannis [Adaptavist]
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.
June 5, 2018

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)
}
craig rayner June 5, 2018

Thanks Thanos thats perfect :)

0 votes
craig rayner May 23, 2018

Does anyone know a solution for this?

Stephen Cheesley _Adaptavist_
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.
May 23, 2018

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

craig rayner May 23, 2018

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.

TAGS
AUG Leaders

Atlassian Community Events