Delete an Issue among several other issues

Hi everybody,

I am trying to develop a post function called after the creation of an issue (post function in my workflow). The goal of my script is to create one or many other issues (clones) according to the completed fields of the original issue created. At the end of the script, the original issue should be delete.

Here my code :


import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueImpl
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def componentManager = ComponentManager.getInstance()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ProjectManager projectMgr =ComponentAccessor.getProjectManager()
issueFactory = ComponentAccessor.getIssueFactory()
ApplicationUser currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issue = issue
//def issue = ComponentAccessor.getIssueManager().getIssueObject("PSD-1904")
CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager()

CustomField cf = cfManager.getCustomFieldObject((long)26904) // Details of the Request PP
CustomField backJava = cfManager.getCustomFieldObject((long)27005) // Back Java PP
CustomField backPhp = cfManager.getCustomFieldObject((long)27004) // Back PHP PP
CustomField front = cfManager.getCustomFieldObject((long)27003) // Front End PP
CustomField epicLink = cfManager.getCustomFieldObject((long)19901) // EpicLink PP
CustomField product = cfManager.getCustomFieldObject((long)26900) // EpicLink PP

def cfValueFront = issue.getCustomFieldValue(front)
def cfValueBackPhp = issue.getCustomFieldValue(backPhp)
def cfValueBackJava = issue.getCustomFieldValue(backJava)
def cfValue = issue.getCustomFieldValue(cf)

def createMainIssue2(def productCF, def epicLinkCF, def issueFactory, String var,def issue, ApplicationUser myUser,IssueManager issueManager){

newissue = issueFactory.getIssue()
newissue.setSummary (var + issue.summary)
newissue.setProject (issue.project)
newissue.description = issue.description
newissue.reporter = issue.getReporter()
newissue.assignee = issue.getAssignee()
newissue.priority = issue.getPriority()
params = ["issue":newissue]
def changeHolder = new DefaultIssueChangeHolder();
def epicLinkValue = issue.getCustomFieldValue(epicLinkCF)
epicLinkCF.updateValue(null,newissue,new ModifiedValue(newissue.getCustomFieldValue(epicLinkCF),epicLinkValue),changeHolder)
def productValue = issue.getCustomFieldValue(productCF)
productCF.updateValue(null,newissue,new ModifiedValue(newissue.getCustomFieldValue(productCF),productValue),changeHolder)
return true

if(cfValue != null){
for(int i = 0;i < cfValue.size();i++){
if( (String) cfValue.get(i) == 'Catalog - New Product'){
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Product Label Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Product Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Pricing Review) : ",issue,currentUserObj,issueManager)

else if( (String) cfValue.get(i) == 'Adaptive Pricing / MVPT'){
createMainIssue2(product, epicLink, issueFactory,"Adaptive Pricing / MVPT (Rules Creation) : ",issue,currentUserObj,issueManager)

else if( (String) cfValue.get(i) == 'AB Test'){
createMainIssue2(product, epicLink, issueFactory,"AB Test (ABTest Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"AB Test (BVO - ABTest Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"AB Test (ABTest Closure) : ",issue,currentUserObj,issueManager)


else if( (String) cfValue.get(i) == 'New RateCard'){
createMainIssue2(product, epicLink, issueFactory,"New Ratecard (FrontEnd Integration) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"New Ratecard (FrontEnd Configuration) : ",issue,currentUserObj,issueManager)

else if( (String) cfValue.get(i) == 'New Activation Service (Privileges and Rights)'){
createMainIssue2(product, epicLink, issueFactory,"New Activation Service (Privileges and Rights) : ",issue,currentUserObj,issueManager)

else if( (String) cfValue.get(i) == 'New Web Service'){
createMainIssue2(product, epicLink, issueFactory,"New Web Service : ",issue,currentUserObj,issueManager)

if(cfValueBackJava != null){
String myVar = new String()
myVar = "Back Java :"
for(int i =0; i< cfValueBackJava.size();i++){
myVar = myVar + " "+cfValueBackJava.get(i)+"/"
myVar = myVar + " : "
createMainIssue2(product, epicLink, issueFactory,myVar,issue,currentUserObj,issueManager)

if(cfValueBackPhp != null){
String myVar2 = new String()
myVar2 = "Back PHP :"
for(int i =0; i< cfValueBackPhp.size();i++){
myVar2 = myVar2 + " "+cfValueBackPhp.get(i)+"/"
myVar2 = myVar2 + " : "
createMainIssue2(product, epicLink, issueFactory,myVar2,issue,currentUserObj,issueManager)

if(cfValueFront != null){
createMainIssue2(product, epicLink, issueFactory,"Front New Web Service : ",issue,currentUserObj,issueManager)

issueManager.deleteIssue(currentUserObj, issue,
EventDispatchOption.ISSUE_DELETED, false);


The creation of the other issues (clones) works well.

The main point that I don't understand is that my script (post function) is called several times ((number of clones created+1) calls).

The second point which is link to the first one, is that the script delete all my cloned issues and not my original issue.

Thanks for your help.

1 answer

1 vote
Joseph Pitt Community Champion May 31, 2017

Deleting issues is a bad idea. The forum is full of questions about restoring them. I close with a resolution of Deleted. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,412 views 15 19
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you