Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I add an attachment to an issue in a post-function?

Ideally, I want to add 1 of 4 attachments based on what a certain field value. I was trying to mess around with the "Set field value (JMWE add-on)" post-function, but I haven't made too much progress thus far.

3 answers

Hi @Cole,

 

So you have the 4 attachments stored on your server and want to attach ne of them?

Or where do you get the attachments from?

 

I'm not used to JMWE, but I once did something similar with Script Runner.

There I generated a text file within the script and attached that file to the issue.

If you are interested in this, I could look up how I did that.

I appreciate the reply!

I was unsure where was the best place to have the files. At the moment they are on my PC. Do they have to be on the Jira server?

I am really open to suggestions here!

I don't think that Jira can access the files on your computer because the post function is happening on the server and the server then don't know, where to find the files. So I think the files must be stored on the server. 

That makes sense. I was wondering if Jira could somehow access them on a network drive as I can access them buy putting the path in a browser url.

But if it has to be on the server, then I can work with that. Can you share the script you used so I might get some ideas of where to start?

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


def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.getUser()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject('IS-1')

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10302")
def cFieldValue = issue.getCustomFieldValue(cField)
log.warn(cField)
log.warn(cFieldValue)
if (cFieldValue != null){
def String filename = ""+cFieldValue + ".txt"

File f = new File("/tmp/"+filename);

f.getParentFile().mkdirs();
f.createNewFile();


FileWriter fw = new FileWriter("/tmp/"+filename, true)
BufferedWriter bw = new BufferedWriter(fw)
PrintWriter out = new PrintWriter(bw)

out.println("Some Text");

out.close()

def bean = new CreateAttachmentParamsBean.Builder()
.file(f)
.filename(filename)
.contentType("text/plain")
.author(user)
.issue(issue)
.build()
attachmentManager.createAttachment(bean)

Same idea, but for a JMWE Scripted (Groovy) post-function:

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

if (!issue.get("your custom field"))
return;

def filename
switch (issue.getAsString("your custom field")) {
case "value 1":
filename = "file 1.txt"
break
case "value 2":
filename = "file 2.txt"
break
}

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("<path to files>/"+filename))
.filename(filename)
.contentType("text/plain")
.author(currentUser)
.issue(issue)
.build()
ComponentAccessor.attachmentManager.createAttachment(bean)

 Please note that I didn't test this code, so there might be bugs :(

Hi David,

Thank you very much! Do the files have to be on the JIRA server or could they be on a network drive or elsewhere potentially? 

They need to be accessible by the Jira process. So technically they can be on a network drive if that drive is mapped on your server and accessible by the user that is used to run Jira. 

That makes sense. Thank you very much for the help!

Hi @David Fischer _Appfire_ 

Using the above-provided code, I am successfully able to attach a file to the issue(Giving absolute file loc like ".file(new File("C:/temp"+filename))").

However, I need to browse the file from <input type="file">. This attribute only returns file name, not the complete location of a file because of security risk and without the full location of the file, code generates an error.

Could you please lemme know how I can proceed with this like how to pass file object in file method.

How are you adding the input element in the transition screen?

Hi @David Fischer _Appfire_ 

I have created a custom button on Jira issue screen using script runner and after clicking on the button, a pop-up screen has appeared. On this screen, I have added a file attribute.

And on form submission, I need to create an issue and attach a file with it. 

Rest task is completed. All I am stuck is getting the file location.

 

This is likely handled by Tomcat, but I am really shooting in the dark here. I'm not even sure you will be able to access the uploaded file. 

Hello All,

 @Bastian Stehmann _neusta portal services_ 

 

I need to copy attachments from one of the issue in project to new issue using post function. I am using on-demand jira and have script runner.  Please help me !!!.

Thanks in advance.

 

Regards,

Ishant

I have successfully attached the file using script runner and i created file in efs and attach the file to issue. This is the code.

packages 

import   com.atlassian.jira.issue.Issue;import import  com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import  com.atlassian.jira.issue.AttachmentManager

code

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("Test-123")
issue.setSummary("Test user detils")
ApplicationUser user = userManager.getUserByName("ravi")

def newFile = new File("/efs/jira-data-shared/data/attachments/764401")
newFile.write("attaching a file to an issue")

def attachmentManager = ComponentAccessor.getComponent(AttachmentManager)

def bean = new CreateAttachmentParamsBean.Builder()

bean.file(newFile)
bean.filename("project_details.txt")
bean.contentType("text/plain")
bean.author(user)
bean.issue(issue)

attachmentManager.createAttachment(bean.build())

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

New cloud apps roundup - June 2021

Since our last roundup in April, Atlassian's Marketplace Partners have added over 100 new cloud apps to the Atlassian Marketplace to help your teams work more efficiently. Let’s take a quick look a...

469 views 5 12
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