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

Marco Brundel
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.
July 9, 2021

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

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2021

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

Marco Brundel
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.
July 9, 2021
0 votes
Bohdan Lozinskyi June 13, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

There is a custom condition-script which checks if there is an attachment at the level of the entire ticket:

import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.attachmentManager.getAttachments(issue).size() >= 1

 

The question is, is it possible to create a simple condition-script that checks whether the attachment was added during the transition only.

0 votes
Caoimhin Caldwell
Contributor
January 4, 2022

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2022

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

Caoimhin Caldwell
Contributor
January 9, 2022

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 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2022

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
Caoimhin Caldwell
Contributor
January 10, 2022

Hi Ram,

 

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

 

Regards

 Caoimhin 

Caoimhin Caldwell
Contributor
January 12, 2022

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2022

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

TAGS
AUG Leaders

Atlassian Community Events