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,365,241
Community Members
 
Community Events
168
Community Groups

How to find latest attachment in an jira issue and attach to the email notification

I am using email this issue script runner post function. I need to find last/latest attachment in an issue and email that as part of closure notification. I found below script and its working for pdf format, but i tried deriving for all format but it couldn't.

Can any one help ?

----------------

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(".jpg")
it.filename.endsWith(".png")
it.filename.endsWith(".xlsx")

it.filename.endsWith(".pdf")

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

return latest.id == a.id

2 answers

1 accepted

1 vote
Answer accepted

My requirement was to fetch issue details, latest comments and latest attachment and send a closure notification to the issue filed "email". With help of Ram Kumar (atlassian admin) was able to fetch few helps and i finally composed a complete solution.

I have used Script runner (email this issue) post function.

 

Custom attachment callback: (to fetch latest comments in listed formats)

mport com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.MailAttachment
{
MailAttachment a ->
def issue = a.issue
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
}

But when i use this along with my other template and try completing the ticket - its throwing an error. 

Done-Transitions.JPG

i am using Email Issue Post-function  where i have configured subject, HTML Body and under attachment options chosen - Attachments returned by a Groovy script as shown below. Kindly advise where i made a mistake.

attachment groovy.JPG

Suggest an answer

Log in or Sign up to answer