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

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

How to Include the last added attachment in a comment by a postfunction

With a Scriptrunner Post function I want to include the last added attachment in a comment.

Is this possible?
If so, what value should I use to determine the last added attachment and include it in the comment?



1 answer

1 accepted

Hi @Marco Brundel

For your requirement, you can try the working code below:-

import com.atlassian.jira.component.ComponentAccessor

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def attachmentManager = ComponentAccessor.attachmentManager
def commentManager = ComponentAccessor.commentManager

def attachments = attachmentManager.getAttachments(issue)

def latestAttachment = attachments.findAll().sort { it.created }.last()

def commentBody = "file has been added to the comment"

commentManager.create(issue,loggedInUser, "[^${latestAttachment.filename}] ${commentBody}", false)

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

What this code does is that it will sort the attachments according to the time when it was created and will only take the last attachment when it transitions. 

Below are the steps to configure the post function:-

1) Once you have selected which transition will trigger the post function, you will need to add a new post-function as shown below:-


2) You will need to select the Custom script post-function [ScriptRunner] option and click the Add button as shown below:-


3) Next, add the sample code to the Post-Function and click on the Update button as shown below:-


Next, publish the changes.

Below are a few test print screens:-

1) First, add your attachments to the ticket as shown below:-


here the test1.txt attachment was added after the sample1.txt attachment

2) Next, to trigger the Post-Function, the issue has to transition to In Progress as shown below:-


as expected, the last file is added to the comment.

I hope this helps to solve your question. :)

Thank you and Kind Regards,


Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

115 views 1 12
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