Forums

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

ScriptRunner - Adding Ticket ID to attached attachment(s)

Michael
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.
October 18, 2023

Hi everyone,

I'm trying to figure out how to use the Custom script scriptrunner post-function to look for attachements within the create screen, and after the issue is created, add the Jira ticket ID to the front of the attachement(s) name(s).

So far I have this:

import org.ofbiz.core.entity.GenericValue

import com.atlassian.jira.ofbiz.OfBizDelegator

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.attachment.Attachment


def attachmentManager = ComponentAccessor.getAttachmentManager()

OfBizDelegator delegator = ComponentAccessor.getComponentOfType(OfBizDelegator.class)

issue.get("attachment")?.each {

  for(GenericValue attachment : delegator.findByField("FileAttachment", "id", it.id)){

      attachment.setString("filename", issue.summary+it.filename)

      attachment.store()

  }

}

but I can't seem to get this to work. Can anyone help with this?

Thanks,

Mike

2 answers

1 accepted

0 votes
Answer accepted
Michael
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.
October 19, 2023

Hi all,

We figured out how to re-name / pre-append a Jira Ticket ID to all attachments that are added at the create step via the code below. Hope this helps someone else out.

 

import org.ofbiz.core.entity.GenericValue;

import com.atlassian.jira.ofbiz.OfBizDelegator;

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.attachment.Attachment;

//def attachmentManager = ComponentAccessor.getAttachmentManager();

OfBizDelegator delegator = ComponentAccessor.getComponentOfType(OfBizDelegator.class)

def issueID = issue.key

issue.getAttachments()?.each {

  for(GenericValue attachment : delegator.findByField("FileAttachment", "id", it.id)) {

    // if filename does not start with ticket key add it

    if (!it.filename.startsWith(issueID)) {

      attachment.setString("filename", "${issueID}_${it.filename}")

      attachment.store()

    }

  }

}
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
October 19, 2023

Hi @Michael

From your description, it appears you are trying to rename the attachment name as they are added to the issue. Please confirm.

I am asking this so I can try to provide with a sample solution.

Thank you and Kind regards,
Ram

Suggest an answer

Log in or Sign up to answer