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,301,530
Community Members
 
Community Events
165
Community Groups

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?

 

Greetings, 
Marco

2 answers

1 accepted

0 votes
Answer 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:-

add_post_function1.png

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

add_post_function2.png

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

add_post_function3.png

Next, publish the changes.

Below are a few test print screens:-

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

test1.png

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:-

test2.png

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

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Hi Ram,

 

Maybe you could help me as my needs are quite similar to what you have posted above. I simply need every attachment added also added as a comment. Could this be achieved through an easier method as its quite straight forward or with an adjustment to the script above?

Thanks

Caoimhin

Hi @Caoimhin Caldwell

For your requirement, you can pretty much use the same code provided in the example in my previous comment.

But instead of adding the filter only to take the last attachment, i.e.

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

You will need to exclude the last() option as below:-

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

This is so that it will take all the attachments as a list instead of only the last attachment.

Next, you will need to iterate through the list and add the attachments like below:-

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

Below is the complete updated sample code for your reference:-

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 }

def commentBody = "all file(s) have been added to the comment"

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

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram 

 

Thanks a lot for that answer, really helpful going to try it today. Just one other question if you don’t mind, when you said above that the script isn’t 100% as differs for environment . What are the variables on this that I need to change custom to my Jira instance? Using server by the way

 

Thanks again 

Hi @Caoimhin Caldwell

You will need to check, for example, what are the actual field names you are using in your environment.

If, say, it is a REST Endpoint example that may require authentication, you must double-check the credentials used.

Thank you and Kind Regards,

Ram

Like # people like this

Hi Ram,

 

This has worked , thanks very much for your help much appreciated 

 

Regards

 Caoimhin 

Hi Ram, 

 

It is actually failing to run for me, I have attached the failed log if you help I would appreciate it scriptrunner.png

Hi @Caoimhin Caldwell

From the error message that you have shared, if you have not made any modifications to the code, it appears to be complaining about line 7 of the code, i.e.

def attachments = attachmentManager.getAttachments(issue) 

You need to double-check if that issue has any attachments. If not, then it is expected that that error message will be returned.

Please share the updated code that you are using.

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

📅 Partner Webinar & Events Opportunities: July 2022

Hi community 👋, Here is another monthly roundup of the partner-organized events in July 2022. If there is any event missing please let me know in the comments below 🙏 If you're looking for Atla...

29 views 0 2
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