Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,362,193
Community Members
 
Community Events
168
Community Groups

Get last added attachment to issue with Scriptrunner

Edited

Hi

I need to get from the issue last added attachment. Do you know how I can achieve this? I'm able to get list of issue attachemnts but this list is sorted by name.

 

Regards,

Seba

1 answer

1 accepted

2 votes
Answer accepted

Just sort the attachment collection by created attribute:

import com.atlassian.jira.component.ComponentAccessor
def im = ComponentAccessor.issueManager
def am = ComponentAccessor.attachmentManager
def issue = im.getIssueObject('JSP-4736')
am.getAttachments(issue).sort{it.created}.last()

@Peter-Dave Sheehan thanks for the answer, i am figuring a way to sort the attachments (irrespective of file format) and attach to the email notification as part of script runner post function. Kindly assist me.

In your above method seems like its just sorting but not fetching the latest file ?

 

I found below method but couldn't derive for all format:

import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.MailAttachment
{
MailAttachment a ->

def issue = a.issue

def allAttachments = issue.attachments

def latest = allAttachments.findAll {

it.filename.endsWith(".docx")
it.filename.endsWith(".pdf")
it.filename.endsWith(".jpg")
it.filename.endsWith(".png")
it.filename.endsWith(".xlsx")

}.sort { it.created }.last()

return latest.id == a.id

Correct, this question was just about sorting and retrieving that last attachment object. Nothing was mentioned about retrieving the actual file content.

@Peter-Dave Sheehan thanks for the response. As requested above, I am figuring a way to sort the attachments (irrespective of file format) and attach to the email notification as part of script runner post function. Please assist me with this.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events