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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,552,839
Community Members
 
Community Events
184
Community Groups

Get last added attachment to issue with Scriptrunner

Edited
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.
Sep 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

2 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 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.
Sep 15, 2020

@Peter-Dave SheehanThanks Peter!

@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
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 11, 2022

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.

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events