Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Add attachments to newly created issue

Alexandr Trifonov January 11, 2023

I'll use this code for custom buttons:

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()

if (getBehaviourContextId() == "link-create-blocking") { getFieldById("project-field").setReadOnly(true)
getFieldById("issuetype-field").setReadOnly(true)

def contextIssue = issueManager.getIssueObject(getContextIssueId())

getFieldById("summary").setFormValue("Issue created from ${contextIssue.key}").setReadOnly(true)
getFieldById("issuelinks-linktype").setFormValue("blocks").setReadOnly(true) getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true)
}


Is it possible to somehow add attachment copying?

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2023

Hi @Alexandr Trifonov

Could you please clarify exactly when you want to add the attachment to the issue? Is it when you are creating the issue or immediately after the issue has been created?

I am asking this because the sample code you are using appears to be the Behaviour code which is only applicable during the Create / Edit  / Transition screens.

If it is the latter, it would be best to use the Post-Function for the create transition instead.

Kindly clarify so I can suggest a better approach.

Thank you and Kind regards,

Ram

Alexandr Trifonov January 13, 2023

It would be ideal when creating. Any possible to copy only description's attachment?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2023

Hi @Alexandr Trifonov

For your requirement, the Behaviour won't be test best solution. It would be best if you instead used the Post-Function.

Below is an example code for your reference:-

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

def attachmentManager = ComponentAccessor.attachmentManager
def file = new File('/home/ram/Pictures/image1.png')

def attachmentParams = new CreateAttachmentParamsBean(file, file.name, 'image', null, issue, false, true, null, new Date(System.currentTimeMillis()), true)
attachmentManager.createAttachment(attachmentParams)

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are the screenshots of the Post-Function configuration:-

config_1.png

config_2.png

If you observe the first screenshot above,  you will notice that the Post-Function is included in the Create transition below the FireEventFunction method. This is required for the Post-Function to work.

Below is a test screenshot:-

test1.png

If you observe the screenshot above, the ticket automatically includes the attachment when the issue is created.

I hope this helps solve your question. :-)

Thank you and Kind regards,
Ram

Alexandr Trifonov January 18, 2023

We've got like a thousand workflows and this gonna be really difficult to edit all of it. Is there a way to do this exactly with Behaviour?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 19, 2023

Hi @Alexandr Trifonov

In your last comment, you asked:-

We've got like a thousand workflows and this gonna be really difficult to edit all of it. Is there a way to do this exactly with Behaviour?

To answer your question, no, this is not possible. There is no simple and easy for this.

If you want the attachment to be automatically added, this is not doable via the Behaviour. Why? Because using Behaviour while creating the issue, the issue does not exist yet; it cannot invoke the underlyingIssue because it will return a null value.

Also, to add an attachment to an issue, you must use the AttachmentManager and CreateAttachmentParamsBean. The CreateAttachmentParamsBean requires the issue variable shown below, which is not accessible via the Behaviour in the Create screen.

def attachmentParams = new CreateAttachmentParamsBean(file, file.name, 'image', null, issue, false, true, null, new Date(System.currentTimeMillis()), true)
attachmentManager.createAttachment(attachmentParams)

If you insist on using Behaviour, you can only add the attachment after creating the issue, i.e. using the Edit screen.

You will need to invoke the issue using the underlyingIssue variable, i.e. something like the below on the Behaviour Initialiser:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def attachmentManager = ComponentAccessor.attachmentManager
def file = new File('/home/user/Pictures/image1.png')

if (underlyingIssue) {
def attachmentParams = new CreateAttachmentParamsBean(file, file.name, 'image', null, underlyingIssue, false, true, null, new Date(System.currentTimeMillis()), true)
attachmentManager.createAttachment(attachmentParams)
}

But I cannot assure you that this will work as you expect. Any time you refresh your page, duplicates of the attachment can be created, and they cannot be removed unless the Behaviour is disabled.

Hence, the safest way for your requirement is to use the Post-Function, as I mentioned in my previous comment.

I hope this answers your question. :-)

Thank you and Kind regards,

Ram

Alexandr Trifonov January 23, 2023

@Ram Kumar Aravindakshan _Adaptavist_ , Thank you very much for your time and suggestions!
Once again i am convinced that the atlassian community is very responsive :)

TAGS
AUG Leaders

Atlassian Community Events