The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I want to add an attachment from JIRA server to every Task using post function

Prashant Karakanagoudar
December 17, 2020

Hi Team,

 

Can someone please help me to add an attachment using scripted post function?

I used the suggestion in below link and it looks like the attachment is getting deleted after the script execution, how can I ensure the script does not delete the file from the location.

https://community.atlassian.com/t5/Answers-Developer-Questions/Is-there-a-way-to-add-an-attachment-with-automation-on-Subtask/qaq-p/467766

 

def attachmentManager = ComponentAccessor.getAttachmentManager()
def pathManager = ComponentAccessor.getAttachmentPathManager()
def user = ComponentAccessor.getJiraAuthenticationContext()?.getLoggedInUser()

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("/appl/jira_shared/test.txt"))
.filename("text.txt")
.contentType("plain/txt")
.author(user)
.issue(issue)
.build()
attachmentManager.createAttachment(bean)

 

Regards,

Prashant

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Answer accepted
mogavenasan
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 Champions.
January 3, 2021

Hi Prashant,

Can you try the following code and see if it works.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean

def attachmentManager = ComponentAccessor.getAttachmentManager()
def pathManager = ComponentAccessor.getAttachmentPathManager()
def user = ComponentAccessor.getJiraAuthenticationContext()?.getLoggedInUser()

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("/appl/jira_shared/test.txt"))
.filename("text.txt")
.contentType("plain/txt")
.author(user)
.issue(issue)
.copySourceFile(true)
.build()

attachmentManager.createAttachment(bean)

You may refer to https://docs.atlassian.com/software/jira/docs/api/8.5.5/com/atlassian/jira/issue/attachment/CreateAttachmentParamsBean.html for more details on CreateAttachmentParamsBean.

I hope this helps.

Cheers,
Moga

Edited based on the reply. Thanks!

Prashant Karakanagoudar
January 3, 2021

Hi Moga,

 

Thanks a lot for your inputs here.

It worked. :-) 

 

Changes the copySourceFile with Boolean value instead of a string.

.copySourceFile(true)

 

 

Prashant

Like mogavenasan likes this
DEPLOYMENT TYPE
SERVER
VERSION
8.5.5
TAGS
AUG Leaders

Atlassian Community Events