Create multiple tickets with one attachment from one ticket with multiple attachments

anadj84 June 23, 2017

Hi all,

from our external customer we are receiving tickets with multiple attachments which are hard to process for our accounting department. In order to make their life easier, for every ticket with more than 1 attachment we want to create/copy as many tickets as there are attachments with inheriting all the fields from the source ticket and of course only one attachment.

Does somebody have a suggestion how to do it, with maybe Script Listenner for the IssueCreated event or PostFunction on the Create transition?

Is this even a good idea ? :-)

 

Thank you,

Ana

1 answer

1 accepted

1 vote
Answer accepted
Daniel Yelamos [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 27, 2017

Hello Ana. 

You can have this done in a different ways.

1. As you said, you can have it setup as a listener. Basically everytime you receive a IssueCreated event, you would inspect the attachments, and create one subtask per attachment. 

2. The second option, which I think it might be "lighter" than the listener, would be to have a postfunction on an specific transition, for example, when you switch to "In Progress" than when transitioned, creates subtasks per attachments.

A quick glance at the script would be:

def attachmentList = ComponentAccessor.attachmentManager.getAttachments(issue)
if(attachmentList.size()<2){
  return
}
else{
  attachmentList.each{
   ..... 
   [here you would create an issue and add the attachments]
   ......
   }
}
ComponentAccessor.attachmentManage.deleteAttachmentDirectory( issue)

}

I by no means intend the above script to be an exact script. But that should guide you to create the script you need.

Adressing the "is this a good idea?" question. Think about it. If you are getting 1 issue creation, and if on average you have 3 attachments, you are increasing the average load of the application by 4. If you are packing a punch, that shouldn't be a issue, but it could be one if your server is small or if you have a lot of clients-customers. 

I would recommend option 2, and make it on transition, it will make that transition slow, but it would remove an unecessary load on your instance until the point at which that task creation is really needed(which is only when it is "In Progress", in my humble opinion).

If I can help you with anything else please don't hessitate to ask.

Good day and good luck!

DYelamos

anadj84 July 9, 2017

Hi Daniel,

thank you very much for your answer, it helped a lot. I escpecially appreciate your comment to put it on transition to reduce the load. Here is my solution which works (I am sure there is some classier way to do it, but this works for me for now ;-))

import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParameters

def issue = event.issue as Issue
def attachmentManager = ComponentAccessor.getAttachmentManager()
def numberAttachments = attachmentManager.getAttachments(issue).size()
def attachments = attachmentManager.getAttachments(issue)

Project project = ComponentAccessor.getProjectManager().getProjectObjByKey("TEST") 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueService issueService = ComponentAccessor.getIssueService();
CustomField category = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Category"}
def value = issue.getCustomFieldValue(category)

MutableIssue newIssue = ComponentAccessor.getIssueFactory().getIssue()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

if (numberAttachments < 2)
{
return
}
else
{
for (Attachment attachment : attachments){

issueInputParameters.setProjectId(project.getId())
issueInputParameters.setDescription(issue.getDescription());
issueInputParameters.setPriorityId(issue.getPriority().getId());
issueInputParameters.setSummary(issue.getSummary() + " " + attachment.getFilename());
issueInputParameters.setIssueTypeId(issue.getIssueTypeId());
issueInputParameters.setReporterId(issue.getReporterId());
issueInputParameters.setAssigneeId(issue.getAssigneeId());
issueInputParameters.addCustomFieldValue(category.getId(), '10510');

IssueService.CreateValidationResult result = issueService.validateCreate(issue.getCreator(), issueInputParameters);
if(!result.isValid()) {
log.error("could not create issue: " + result.getErrorCollection());
}

newIssue = issueService.create(issue.getCreator(), result).getIssue();
attachmentManager.copyAttachment(attachment, issue.getCreator(), newIssue.getKey()); 
}
}

 

Like Damian Piñones likes this
Daniel Yelamos [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.
July 10, 2017

Hello Ana.

Thanks very much for posting your answer, I'm sure other users will find that useful. 

Would you kindly mark my answer as accepted so that users know that this question as been answered?

Cheers!

Dyelamos

Like Damian Piñones likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events