Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Copy attachment from task to linked issue during a transition

Hi,

 

I want to copy all attachment from task to linked issue during transition. I am using script runner to create the linked issue and i want to add the attachments from the task.

 

Please provide me with a script.

 

Thanks,

Swarna

1 answer

Hi,

 

I am getting error :

//iterate over the issues linked to the current issue through the "blocks" link type - change the link type as needed
for (Issue linkedIssue : issue.getLinkedIssues("relates to")) {
//add new attachments from linked issue
def linkedAttachments = linkedIssue.get("attachment")
for (Attachment linkedAttachment : linkedAttachments) {
//test if this attachment from the linked issue already exists on the current issue
if (!attachments.find{it.filename == linkedAttachment.filename && it.filesize == linkedAttachment.filesize && it.mimetype == linkedAttachment.mimetype})
//this attachment is new, copy it to the current issue
ComponentAccessor.attachmentManager.copyAttachment(linkedAttachment, currentUser, issue.key)
}
}

Can you please let me know the error. what you are getting

Hi,

Capture.PNGPlease see screenshot.

Thanks

can you please  include below at the top and try again

import com.atlassian.jira.issue.Issue

Still the same

 

Capture 2.PNG

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
linkedIssues.each {

def linkedIssue = it.destinationObject

attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

}

 

try above code, it will help 

Hi,

 

I have tested the above code. No attachments are being copied. 

Please check whether you have add attachment permission on linked issues or not.

 

You can check logs for any error. or Add log statements to the code, you can find whether the issue is.

Hi,

I have the permission to add attachments on linked issues. The script runs successfully.  There is no error in the logs.

Thanks

In this code , i am checking only outward links. can please check the links are outward or inward.

Other you can add log or system. out. println statement to check execution.

 

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
System.out.println("Current issue" + issue.getKey());
System.out.println("linked issues size " + linkedIssues.size());

linkedIssues.each {

def linkedIssue = it.destinationObject
System.out.println("linked issue >>>>" + linkedIssue.getKey());

attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

System.out.println("done");

}

Hi,

Time (on server): Tue Aug 20 2019 10:52:04 GMT+0400 (Gulf Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

No logs were found for this execution.

 

No logs are being found even I have changed Inwards and Outward linked

Hi,

Is there any solution?

Hi, I have added logs:

 

Time (on server): Wed Aug 21 2019 09:51:42 GMT+0400 (Gulf Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-08-21 09:51:42,719 DEBUG [workflow.ScriptWorkflowFunction]: Current issue: CM-197
2019-08-21 09:51:42,720 DEBUG [workflow.ScriptWorkflowFunction]: linked issues size : 0

 

 

The linked issues size is 0 and for linkedIssue.getKey() there is no logs.

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def attachmentManager = ComponentAccessor.getAttachmentManager();

String commentAuthor = "currentUser"
def authorUser = (commentAuthor == "currentUser") ? ComponentAccessor.jiraAuthenticationContext.loggedInUser : ComponentAccessor.userManager.getUserByName(commentAuthor)
System.out.println("Current issue" + issue.getKey());

log.debug "Current issue: ${issue.getKey()}"
System.out.println("linked issues size " + linkedIssues.size());

log.debug "linked issues size : ${linkedIssues.size()}"

linkedIssues.each {

def linkedIssue = it.destinationObject
System.out.println("linked issue >>>>" + linkedIssue.getKey());
log.debug "linked issue >>>>: ${linkedIssue.getKey()}"
attachmentManager.copyAttachments(issue, authorUser, linkedIssue.key);

System.out.println("done");

}

The reason why you get 0 for the size is because  of getOutwardLinks. Try getInwardLinks instead.

This is how we are copying the attachments from the source issue to a newly created linked issue:

import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level

def linkedIssuesOutward = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id);
def linkedIssuesInward = ComponentAccessor.issueLinkManager.getInwardLinks(issue.id);

def attachmentManager = ComponentAccessor.getAttachmentManager();
log.setLevel(Level.DEBUG)

log.info "Current issue: " + issue.key
log.info "linked issues outward size: " + linkedIssuesOutward.size()
log.info "linked issues inward size: " + linkedIssuesInward.size()

linkedIssuesInward.each {

def linkedIssue = it.sourceObject

log.info "linked issue >>>>: ${linkedIssue.getKey()}"

attachmentManager.copyAttachments(linkedIssue, issue.creator, issue.key);

}

I hope this helps.

Kind regards,

Daphnis

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

260 views 7 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you