ScriptRunner condition to ensure only one sub-task can be created per issue

Gavin Minnis July 30, 2018

I use ScriptRunner Script Listeners and Post Funtions to create sub-tasks.

However, with this particular workflow, I need a condition that checks to see if any other sub-tasks of the same type have already been created. If they have, then I need JIRA to restrict the action to ensure that only one sub-task per sub-task type can be created.

My project has 4 different sub-task types: Order Parts, Purge Parts, Claims, and Corrective Actions. 

Can someone help me write a condition for a Script Listener and for a Post Function that checks the parent to see if a similar sub-task has already been created, and if it has restrict the action from completing.

In the end, my workflow should only allow one Order Parts sub-task, one Purge Parts sub-task, one Claims sub-task, and one Corrective Actions sub-task at any given time.

2 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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 30, 2018

Hello @Gavin Minnis

Can you provide more information?

When subtasks created?

What your behaviour do?

Gavin Minnis July 30, 2018

@Mark Markov

Sub-tasks are created at two different times: 1) Issue Create 2) Issue Update, when a field value changes either during a workflow transition (e.g. changing a value on a screen during workflow transition) or simply editing the field on the Issue View or Edit screen.

Regarding your second question, I am so glad that you asked. I didn't mean to say I was using Behaviours. I am actually using a Script Listener. The listener has a condition where it looks for the value of a field to equal "Yes" and then at those times it creates a sub-task. I have the Events for this listener set to Issue Create and Issue Update.

Mark Markov
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 30, 2018

Hmm, I can't provide good advice to you without code examples, but here is example how you can find subtasks of issue and names

import com.atlassian.jira.component.ComponentAccessor

def subtaskToCreateName = "Task"
def issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1")
//getting subtasks names of given issue
def subtaskIssueTypes = issue.getSubTaskObjects().collect {it.getIssueType().getName()}
//checking if creating subtask alrealdy exist
if (!(subtaskToCreateName in subtaskIssueTypes)){
//your code to create subtask
}

Gavin Minnis July 30, 2018

@Mark Markov

I'm not sure what code you need to see. But for the Script Listener I have the Condition set to:

cfValues['Purge Stock']?.getValue() == 'Yes'

So when the custom field "Purge Stock" has a value of "Yes" this listener picks it up and create's a "Purge Stock" sub-task. This listener uses the Issue Create and Issue Update events. So essentially, I'm try to expand this condition so that not only will the Condition be to listen for when the custom field value is changed to "Yes" but also to ensure that there isn't already a "Purge Stock" sub-task created. Because there should only be one at a time. 

Mark Markov
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 30, 2018

Then try this condotion

def subtaskIssueTypes = issue.getSubTaskObjects().collect {it.getIssueType().getName()}
cfValues['Purge Stock']?.getValue() == 'Yes' && !('Purge Stock' in subtaskIssueTypes)
Like Rahul Savaikar likes this
Gavin Minnis July 30, 2018

@Mark Markov

That worked! Perfect. Thanks for your help.

0 votes
siva December 14, 2021

@Mark Markov 

My question is related this script can you please help me how to resolve below issue

https://community.atlassian.com/t5/Jira-Software-questions/How-to-restrict-sub-task-not-created-more-than-one/qaq-p/1886645#U1886922

Thanks

siva

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events