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,293,907
Community Members
 
Community Events
165
Community Groups

Adding an attachment to a comment

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

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

This solution is only for ScriptRunner Server?

Any suggestions to use it on ScriptRunner Cloud ?

Thanks in advance.

0 votes
DPK J Community Leader Aug 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)  

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.

DPK J Community Leader Aug 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.
DPK J Community Leader Aug 21, 2019

You can upvote or accept answer if it helped you.

  • Call updateIssue on issue manager.

What this line exactly does?

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

DPK J Community Leader Aug 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.

Unfortunately nothing happened after I added that line.

DPK J Community Leader Aug 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
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

336 views 0 7
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