Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Get last added attachment to issue with Scriptrunner

Sebastian Krzewiński
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 15, 2020

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

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 15, 2020

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()
Sebastian Krzewiński
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 15, 2020

@Peter-Dave SheehanThanks Peter!

Hemanth Kumar September 11, 2022

@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

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 11, 2022

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

Hemanth Kumar September 11, 2022

@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.

0 votes
Esabel Gomez
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 18, 2023

@Hemanth Kumar - did you ever solve for this? Looking at doing something similar.

Hemanth Kumar April 18, 2023

@Esabel Gomez Yes, i was able to solve with below scripts

My requirement was to fetch latest comments and attachment and respond to user via mail that issue is closed successfully.

<< Below section is for fetching latest attachment irrespective of file formats >>

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

def allAttachments = issue.attachments
def fileExtensions = ['.pdf', '.jpg', '.jpeg', '.docx', '.xlsx', '.png']
def latest = allAttachments.findAll {
fileExtensions.each { String fileType ->
it.filename.endsWith(fileType)
}
}.sort { it.created }.last()
return latest.id == a.id
}

TAGS
AUG Leaders

Atlassian Community Events