Adding an attachment to a comment

Siavosh Kasravi
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.
August 19, 2019

I use this method to add an attachment to an issue:

def attachmentManager = ComponentAccessor.getAttachmentManager()
def bean = new CreateAttachmentParamsBean.Builder(tempFile,
"keylead_"+reqCheckJSON["id"]+".pdf", //resultMap["set"][0]['note']+
'application/pdf',
currentUser,
issue).build();//copySourceFile=false

def changeItemBean= attachmentManager.createAttachment(bean)

 But customers can't see the attachment.

I wondered maybe I should add the attachment as a comment. 

I didn't find a way to make attachment visible to customers. Neither was I able to attach it as a comment.

What do you think is the right way and how?

2 answers

1 accepted

1 vote
Answer accepted
Siavosh Kasravi
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.
August 29, 2019

Found the solution: After attaching the file to issue, a new comment should be added to it containing attached file name by [^ and ] tags like:

Attachment added [^filename.pdf].

Ahsan Nafees Ansari July 1, 2021

This solution is only for ScriptRunner Server?

Any suggestions to use it on ScriptRunner Cloud ?

Thanks in advance.

0 votes
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 19, 2019

Try something like this,

def attachmentManager = ComponentAccessor.getAttachmentManager();
def issueManager = ComponentAccessor.getIssueManager();

CreateAttachmentParamsBean bean = new CreateAttachmentParamsBean.Builder(
tempFile, fileName, 'application/pdf', user, issue).build()
ChangeItemBean changeItemBean= attachmentManager.createAttachment(bean) issueManager.updateIssue(user,issue,new EventDispatchOption.Factory().get(EventType.ISSUE_UPDATED_ID),true)  
Siavosh Kasravi
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.
August 21, 2019

Thanks. Can you please give me a brief explanation on what it does? Or lead me to a book or other resource which explains the matter.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2019

There is no specific book, you can learn it from Jira's java api.

What it does is (line by line),

  • Create instance of attachment manager
  • create instance of issue manager
  • Create new attachment bean object (a placeholder for attachment)
  • Create new change item (as we want to update issue)
  • Call updateIssue on issue manager.
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2019

You can upvote or accept answer if it helped you.

Siavosh Kasravi
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.
August 21, 2019
  • Call updateIssue on issue manager.

What this line exactly does?

I won't forget to upvote after I tested the solution. Thank you.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 21, 2019

What we have done before this like is update issue object in issue to have an attachment associated with it.

But to save these changes that we have to done to issue object has to be transferred to database. So we call,

issueManager.updateIssue(user,issue,new EventDispatchOption.Factory().get(EventType.ISSUE_UPDATED_ID),true)

This method update issue object to database and raise and event so other jira services can know that issue has been updated.

Siavosh Kasravi
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.
August 24, 2019

Unfortunately nothing happened after I added that line.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 24, 2019

I think you need to confirm ScriptRunner team, this seems to be working fine on my instance.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events