How to copy an Attachment from a screen in subtask to the main Issue Edited

 

Hello everyone! I'm trying to copy an attachment from a screen ,
Who is associate to the closure of  a subtask .

I succeeded to do it with comment :

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def originalComment = transientVars.comment as String
def comments = originalComment
if (comments!=null)
{
    commentManager.create(parentIssue, user, comments, false)         
}

I'm trying to do an equivalent with the attachement ( 
copyAttachments(Issue issue, ApplicationUser author, String newIssueKey)
) but

transientVars.attachment

Does not seem to exist
How am I supposed to do ?

2 answers

1 accepted

0 votes
Ivan Tovbin Community Champion Feb 02, 2018

Hi Sergio,

Based on your description, the attachment added during the 'Close issue' transition is always the last one (unless you have any post functions/automation that adds more attachments after that), the code to copy it to the parent issue is as follows:

import com.atlassian.jira.component.ComponentAccessor

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (attachments.size() > 1){
for (int i = 1; i < attachments.size(); i++){
if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
lastAtt = attachments[i]
}
}
}

attMgr.copyAttachment(lastAtt, currentUser, issue.getParentObject().getKey())

 Also, make sure that you place this post function AFTER the "Re-index an issue to keep indexes in sync with the database" post function

Hi Sergio,

 

I would like to do the exact same thing but have no idea on how to manage that.

Hope someone could help :)

 

Best regards,

 

Germain.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,063 views 13 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot