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 vote
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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

197 views 1 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