How to restrict sub:task not created more than one

siva December 13, 2021

Hello team,

In my project we had four Sub:task types: Claim, issue, basket and collect among those tasks i need to manage  only one sub:task "basket" not created  more than one to parent issue.

Here is the example:

sub task.png

We can see above screen shot the Basket Sub: task created twice but need to restrict only one

Note: we are using script runner in our organization 

Please suggest me any suggestions.

Thanks,

Siva.

2 answers

1 accepted

2 votes
Answer accepted
Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2021

Hi @siva 

Use the condition to check for sub-task exists in the parent issue and implement the delete issue automation based on the condition.

https://community.atlassian.com/t5/Jira-Service-Management/How-to-delete-issues-using-Automation/qaq-p/1419344

Thanks,

Pramodh

siva December 13, 2021

Hi @Pramodh M 

Thanks for the early  reply  Unfortunately we don't have Automation plugin in my instance is it possible by using script runner plug-in. Please help me how to achieve this issue.

Thanks,

Siva

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2021
siva December 13, 2021

@Pramodh M 

I'm new to the scriptrunner can you please help me with the  below script 

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
}

 Thanks,

Siva

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Hi @siva 

Please find the Code below to add in Scriptrunner Listeners Option for Issue Created event. Let me know if this works

import com.atlassian.jira.event.issue.DelegatingJiraIssueEvent
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue

if( issue.issueType.subTask ) {
Issue subtaskIssue = issue
log.warn(issue.getParentObject())
parentIssue = issue.getParentObject()
log.warn(parentIssue.subTaskObjects.size())
if ( parentIssue.subTaskObjects.size() == 0 ) {
// Meaning the Parent has either no Sub-task or only 1 Sub-task
log.warn("We are Good")
} else {
log.warn("We are deleting the Sub-task Issue")
log.warn("Total Number of Sub-task: " + parentIssue.subTaskObjects.size())
issueManager.deleteIssueNoEvent(subtaskIssue)
}
} else {
log.warn("This is Standard Issue Type, So we are Good")
}

Thanks,
Pramodh

siva December 14, 2021

Hi @Pramodh M

Thanks for the reply the above script working fine but it restrict all the sub:tasks not created more than one here i need restrict only single sub:task that is "Sub:Basket" and other sub:tasks can create as many as. Please help me with relevant script.

Thanks,

Siva.

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

This should work, replace the Sub-task name with the one you want to restrict

import com.atlassian.jira.event.issue.DelegatingJiraIssueEvent
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue

// Replace the Basket Sub-task Issue Type with one which you want to restrict
if( issue.issueType.name == "Basket" ) {

Issue subtaskIssue = issue
log.warn(subtaskIssue)

log.warn(issue.getParentObject())
parentIssue = issue.getParentObject()
log.warn(parentIssue.subTaskObjects.size())
if ( parentIssue.subTaskObjects.size() == 0 ) {
// Meaning the Parent has either no Sub-task or only 1 Sub-task
log.warn("We are Good")
} else {
log.warn("We are deleting the Sub-task Issue")
log.warn("Total Number of Sub-task: " + parentIssue.subTaskObjects.size())

// log.warn(issueManager.getIssueObject(subtaskIssue))
issueManager.deleteIssueNoEvent(subtaskIssue)
}
} else {
log.warn("This is Standard Issue Type or Sub-task other than the one which you have specified, So we are Good")
}

 Thanks,
Pramodh

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Please accept the Answer If it solved your requirement 🙂

Thanks,
Pramodh

siva December 14, 2021

@Pramodh M 

Unfortunately the  script not restrict the Basket Sub:task i can able to create Basket task more than one. Please look into the script once again thanks for the patience.

Thanks,

Siva

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Can you please give me the exact Sub-task Issue name in double quotes?

Thanks,
Pramodh

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

@siva 

Use the exact Sub-task name from the Issue Type you have created as shown below

Sub-task.png

in the line

if( issue.issueType.name == "Basket" )

Thanks,
Pramodh

siva December 14, 2021

@Pramodh M 

Actually the sub-task is Sub: Grooming and i used exact sub-task in script please see below the screen shot

Here the subtask

Grooming.png

In the line

In the line.png 

Thanks,

Siva

Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

 

It should work, Can you please make sure by editing the Issue Type it doesn't have any whitespaces or extra spaces.

And exact configuration looks like this 

Listener 1.png

And Code is

import com.atlassian.jira.event.issue.DelegatingJiraIssueEvent
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue

log.warn(issue.issueType.name)

if( issue.issueType.name == "Sub: Grooming" ) {

Issue subtaskIssue = issue
log.warn(subtaskIssue)

log.warn(issue.getParentObject())
parentIssue = issue.getParentObject()
log.warn(parentIssue.subTaskObjects.size())
if ( parentIssue.subTaskObjects.size() == 0 ) {
// Meaning the Parent has either no Sub-task or only 1 Sub-task
log.warn("We are Good")
} else {
log.warn("We are deleting the Sub-task Issue")
log.warn("Total Number of Sub-task: " + parentIssue.subTaskObjects.size())

// log.warn(issueManager.getIssueObject(subtaskIssue))
issueManager.deleteIssueNoEvent(subtaskIssue)
}
} else {
log.warn("This is Standard Issue Type or Sub-task other than the one which you have specified, So we are Good")
}

 

Also if it is not working, please send the logs from listener event in Scriptrunner.

Let me know the result. I will have to use the Issue type ID in case this doesn't work

Thanks,
Pramodh

siva December 14, 2021

Replaced the code and still it creating the issues

script.png

Here the log data

2021-12-15 01:33:50,282 WARN [runner.ScriptBindingsManager]: Sub: Grooming
2021-12-15 01:33:50,282 WARN [runner.ScriptBindingsManager]: This is Standard Issue Type or Sub-task other than the one which you have specified, So we are Good

Here the issue created more than one

error.png
Thanks,

Siva

siva December 15, 2021

@Pramodh M 

Good news it starts working after updating  the issue type with no extra space and it restrict the type created more than one. But it shows error message instead of that message can we change to "Can't create task more than one" so users can understand why it happens. Once again big thanks your help.

Popup error message

Sub grooming work (2).png

Thanks,

Siva.

0 votes
siva December 14, 2021

Hi @Vikrant Yadav 

Can you please help me with this issue how to resolve this issue

Thanks,

Siva

Suggest an answer

Log in or Sign up to answer