how to attache attachment in comment

Sneha Gole June 14, 2021

hello,

i want to attach pdf attachment(link) in comment using 

image.png

 

def commentManager = ComponentAccessor.getCommentManager()
def textComment = fnm + " has been added to the issue"
commentManager.create(issue, currentUser, textComment, null, null, new Date(), true)

 

above code create comment as "text" not " pdf link",like below

image.png

2 answers

1 accepted

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.
June 14, 2021

Hi @Sneha Gole 

For your requirement, I assume you are using a Listener to trigger the comment update.

You could try something like this:-

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue

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

def attachments = attachmentManager.getAttachments(issue)

def latestAttachment = attachments.findAll {
it.filename.endsWith(".pdf")
}.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.

Below is a print screen of the listener configuration:-

listener_config.png

Below are a few print screens of the tests:-

1) First, when the issue is created, no attachments are added, as shown below.

issue_created1.png

2) Once an attachment is added, refresh the page, and you will see the comment is automatically added, as shown below.

attachment1.png

3) Adding other pdf attachments, and the comment is updated as well as shown below:-

attachment2.png

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Sneha Gole June 14, 2021

thank you so much for your effort   @Ram Kumar Aravindakshan _Adaptavist_  it work like wonders !!!!

Arkadiusz Baka November 18, 2021

Hi,

@Ram Kumar Aravindakshan _Adaptavist_ comment with lastest attachment is added by every issue update. How to restrict this listener only to add atachment event?

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.
November 27, 2021

Hi @Arkadiusz Baka

To answer your question, this is not directly possible as there are no existing events that monitor when an Attachment is added.

You will need to create a custom Event for this.

Please refer to this community post for more information.

Thank you and Kind Regards,
Ram

0 votes
Niranjan
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.
June 14, 2021

Hi @Sneha Gole ,

What is the def for variable fnm? Your code tries to append fnm and the string.

Sneha Gole June 14, 2021

hii @Niranjan ,

thanks for quick reply ,

i have pass file path in fnm (/opt/atlassian/jira/temp/EMPLOYEEE.pdf)

Niranjan
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.
June 14, 2021

@Sneha Gole - Should you not be using ComponentAccessor.attachmentManager.createAttachment  as you are actually creating an attachment ? 

Niranjan
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.
June 14, 2021
Sneha Gole June 14, 2021

@Niranjan ,

i am attaching pdf file to issue and i want to create comment ___ pdf file is attached .

image.png

Daniel Alonso June 7, 2022

How can I implement this on JSM Cloud?

Aakanksha Gupta May 8, 2023

@Sneha Gole i found this discussion 

and saw your code .

I am able to attach the file but not to comment but to the issue  . Could you help how can we prevent it 

Aakanksha 

Suggest an answer

Log in or Sign up to answer