Forums

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

Adding an attachment when creating a ticket using Scriptrunner

Richard Jenkins
Contributor
February 28, 2023

Hello All!

I am trying to add an attachment (PDF file) to a ticket upon ticket creation. I have engaged the post function "custom script post-function" with the following code:


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

def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.loggedInUser

String pathToFile = "/var/atlassian/application-data/jira/"
String fileName = "ACP620.pdf"
String pathAndFile = pathToFile+fileName

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File(pathAndFile))
.filename(fileName)
.contentType("text/txt")
.author(user)
.issue(issue)
.copySourceFile(true)//you must do this otherwise it deletes the source file from the file system
.build()
attachmentManager.createAttachment(bean)

 


I am getting the "No such file exists" error. Can someone advise on the correct way to do this?

2 answers

1 accepted

0 votes
Answer accepted
Blue Ridge Consultants
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.
February 28, 2023

Can you please confirm that on your Jira server there is a file in this path and it is owned by the user running Jira:

/var/atlassian/application-data/jira/ACP620.pdf
Richard Jenkins
Contributor
March 1, 2023

I actually made a mistake and missed a folder and now got it working. Thanks! 

Is there a way to use a sharepoint file in the "PathtoFile" parameter?

Blue Ridge Consultants
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.
March 1, 2023

Not familiar with SharePoint but maybe the file/folder API is a good starting place for doing that. 

 

https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest

 

As long as you know the file path in SharePoint and can generate a token this should be double.

Richard Jenkins
Contributor
March 2, 2023

Thank you so much! I will try that!

0 votes
Reece Lander _ScriptRunner - The Adaptavist Group_
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.
March 6, 2023

I am glad to hear you worked this out, in ScriptRunner 7.11.0 we introduced HAPI, which makes scripting a lot simpler.

In this case, after upgrading, your post function could be reduced to a single line of code:

issue.addAttachment('/tmp/file.txt')

For more information see:

Cheers :)

Suggest an answer

Log in or Sign up to answer