Help me to add attachment automatically when issue has been created

Musku Nagaraju March 27, 2019

Hi Team,

I have one file in my C drive, when Issue has been created in Project ABC, the file should be attach automatically.

 

I am using below code but not working as I expected.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext

def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.getUser()

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("C:/Applications/Capgemini.jpg"))
.filename("Capgemini.jpg")
.contentType("image/png")
.author(user)
.issue(issue)
.build()
attachmentManager.createAttachment(bean)

3 answers

1 accepted

4 votes
Answer accepted
Matthew Clark
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.
April 2, 2019

Hi Musku

If you create a Scriptrunner "Custom script post-function" on the "Create" workflow transition and set the following script as the Last item on the list of POST functions it will allow you to add an attachment to issues on creation.

Script Link:

Add attachment from the Jira servers file system to the issue on creation

Note: You need to alter the script to fit the file path and name for your system.

Check the atlassian-jira.log if you have problems just in case you get permission errors.

Your original script was missing this line:

.copySourceFile(true)

which means it would work once and then the file would be deleted so all other attempts would fail.

Further information on the CreateAttachmentParamsBean class here.

 

Regards
Matthew

Musku Nagaraju April 2, 2019

Thanks @Matthew Clark  Working fine for me .

Like kanishka likes this
kanishka May 18, 2020

Hi 

I have written the same code but it is showing error (No such file or directory)

@Matthew Clark  

Could you please check this. what's wrong i am doing ?

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

def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.loggedInUser

String pathToFile = "C:/Users/kbharadwaj/Downloads/"
String fileName = "Testing.txt"
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)

kanishka July 1, 2020

@Musku Nagaraju  could you please help me with this ?

Musku Nagaraju July 2, 2020

Hi @kanishka , please fine my code here bitbucket.org/nagarajureddy

let me know if you need any.

 

Thanks

Nagaraju

kanishka July 16, 2020

@Musku Nagaraju  Thanks for reply.

I have tried this code many times. but every time it shows error(No such file or directory).

I have placed file in same place.

Rahul Savaikar December 10, 2021

Hello @Matthew Clark 

 

Thank you - your code works great for a JSD request created.

 

I have one dumb query - can you please provide any suggestions on how to download the automatically attached file to a local system on a simple mouse click, once the request is created.

 

I am asking this, because if the JSD customer clicks on the attachment, they get an encrypted file in the same window, something like the one shown in the attached screenshot.

 

jsd-attachment-error.jpg

shira segal inbar December 15, 2021

@Rahul Savaikar Did you find a solution? I have the same problem...

Rahul Savaikar December 15, 2021

Hello @shira segal inbar 

Nothing so far. I am trying ways to get this resolved.

The only workaround to this is to right-click on the attachment, and use the "Save As..." option.

Regards,

Rahul

shira segal inbar December 16, 2021

Hi @Rahul Savaikar I found the solution :-)

You need to change one of the lines in the code to - 

.contentType("plain/txt")

 That helped me

Thanks,

Shira

Like Rahul Savaikar likes this
Rahul Savaikar December 16, 2021

Hello @shira segal inbar ,

Wow, thanks - this worked.

Perhaps, you can move this as an answer, and I can upvote.

Best Regards,

Rahul Savaikar

D X September 25, 2023

Dear @Musku Nagaraju 

thank you for sharing your code.

 

0 votes
Johannes Rudolf March 17, 2021

Perfect, that is exactly what I was looking for and it works like a charm. In fact had to spent more time to get the file upon the server than to get this post function added to the workflow.

0 votes
Matthew Clark
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 28, 2019

Hi Masku

Can you tell me more about your requirement?

Are you trying to attach the same image to every new issue created or does the attachment change?

Is it always an image?

Is your Jira server running on windows and you want to pull in the file from within the Jira server itself or is this C drive on the client machine?

Musku Nagaraju March 29, 2019

@Matthew Clark ,I am trying to attach same one file in all issues that will never change.

It always file (.doc)

I am trying to pull this from client C drive, also I am fine this to pull from JIRA server as well, Help me on easy way.

 

Thanks

Nagaraju

Musku Nagaraju April 1, 2019

@Matthew Clark , Can you please provide your inputs on this.

Suggest an answer

Log in or Sign up to answer