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
Accepted answer
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 Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,097 views 0 8
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