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,558,901
Community Members
 
Community Events
184
Community Groups

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.
Jul 09, 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

2 answers

1 accepted

0 votes
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.
Jul 09, 2021 • edited

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.
Jul 09, 2021

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.
Jan 09, 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

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.
Jan 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

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

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.
Jan 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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events