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

How to get attachments during issue creation ?

DupontD January 11, 2016

Hello,

In a groovy script, i want to get the attachments list during the issue creation in the first "validators" step (transition "Create").

My script : if a customfield "zzz" has the value "xxx", an attachment is mandatory.

This issue isn't stored in the database yet.

 

I tried with the function : issue.getAttachments()

or : AttachmentManager.getAttachments(issue)

or : changeItems.findAll { item -> item.getField() == "Attachment" && item.getFieldType() == "jira" }

But i always have this kind of error :

 

/secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: /data/jira/groovy/xxx.groovy

java.lang.NullPointerException

        at com.atlassian.jira.issue.managers.DefaultAttachmentManager.getStoredAttachments(DefaultAttachmentManager.java:191)

        at com.atlassian.jira.issue.managers.DefaultAttachmentManager.getAttachments(DefaultAttachmentManager.java:185)

        at com.atlassian.jira.issue.AbstractIssue.getAttachments(AbstractIssue.java:71)

        at com.atlassian.jira.issue.Issue$getAttachments.call(Unknown Source)

 

issue.getCustomFieldValue(xxx) is ok.

 

How can i get the attachments list ?

 

Thanks !

 

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
DupontD January 11, 2016

Thanks Jamie !

 

I tried :

import webwork.action.ActionContext
def request = ActionContext.getRequest()
if (request)
{
    if (request.getParameter("filetoconvert") != null)
    {
        // Attachment(s)
    }
}

It seems to be ok but i don't know if it's the best way.

JamieA
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.
January 11, 2016

If it's working, I think it's the only way...

Like Laith Ata likes this
Isaac_Philip March 29, 2016

Your solution does not work for me!

My custom groovy script code snippet is,

if(attachmentManager.getAttachments(issue).size() == 0)

for which i get the same error as,

java.lang.NullPointerException
 at com.atlassian.jira.issue.managers.DefaultAttachmentManager.getStoredAttachments(DefaultAttachmentManager.java:208)
 at com.atlassian.jira.issue.managers.DefaultAttachmentManager.getAttachments(DefaultAttachmentManager.java:200)
 at com.atlassian.jira.issue.AttachmentManager$getAttachments.call(Unknown Source)
 at Script99.run(Script99.groovy:27)

Please advise

DupontD March 29, 2016

It works for us (JIRA 7.0.5 and 7.0.10).

Script is set in "validators" step / "create" transition in our workflow.

Try to log the "request" variable (request.toString()) and see if you have the "filetoconvert" parameter.

JamieA
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 29, 2016

Isaac, I think you have another problem. Do the attachments display correctly on the screen? What version are you using?

Isaac_Philip March 29, 2016

Thanks Dupont and Jamie for your prompt reply!
Jamie I have seen ur replies quite often and it seems ur like the one man army with ur responses all over the place, let me tell you that its great work!

@Dupont My version is JIRA 6.4.5

Following code is set for the Create Issue transition in the validators section.

import webwork.action.ActionContext
def request = ActionContext.getRequest()
logger.info('request variable')
logger.info(request.toString())
logger.info(request.getParameter("filetoconvert").toString())
 
::Results::
Mar 30, 2016 4:02:53 AM java_util_logging_Logger$info$0 call
INFO: request variable
Mar 30, 2016 4:02:53 AM java_util_logging_Logger$info$0 call
INFO: com.atlassian.plugin.servlet.PluginHttpRequestWrapper@60da04d7
Mar 30, 2016 4:02:53 AM java_util_logging_Logger$info$0 call
INFO: null

Seems filetoconvert does not exist?


@Jamie Yes the attachments do display correctly and get attached too.

FYI,
My task is to perform validation when two custom field dropdowns are selected to certain values, then user needs to attach a file!

 

JamieA
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 30, 2016

Hi... Can you log request.getParameterMap() - see what the keys are. 

Is this on the Create action?

Isaac_Philip March 30, 2016

Hi, Yes this validation i am trying to accomplish is on the Create action.

The log of the function is below

logger.info('request.getParameterMap()')
logger.info(request.getParameterMap().toString())
 
:::Results:::
 
Mar 31, 2016 3:29:18 AM java_util_logging_Logger$info$0 call
INFO: request.getParameterMap()
Mar 31, 2016 3:29:18 AM java_util_logging_Logger$info$0 call
INFO: [decorator:[none], pid:[10200], issuetype:[10100], atl_token:[xyz-abc], formToken:[xyz-abc], priority:[3], description:[-description-], customfield_10803:[10412], customfield_10905:1:[3], customfield_10905:[359], customfield_10804:[10420], summary:[-summary- ], customfield_10802:[10408], customfield_10801:[10402], customfield_10816:[], dnd-dropzone:[], fieldsToRetain:[project, issuetype, priority, customfield_10803, customfield_10905, customfield_10804, customfield_10802, customfield_10801, customfield_10816]]

 

What key are we looking for?

JamieA
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 30, 2016

> What key are we looking for?

Well, I don't know, that's why we're doing this. Is that the output you get when the submission includes newly-added attachment?

Isaac_Philip March 31, 2016

Thanks Jamie.

I understand now after I fiddled with the output after attaching file.

def file_to_convert = request.getParameter("filetoconvert")
if(file_to_convert == null)
    logger.info("file to convert is null so apparently we don't have any attachments! so perform validation!!")

Thanks it works!

Now the next step is for raising the validation error which I'll look into! Thanks Jamie & Dupont!

JamieA
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 31, 2016

ok, well done. You just want to "throw new InvalidInputException(...)"

0 votes
JamieA
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.
January 11, 2016

I don't have a good answer for you. Unfortunately the JIRA API doesn't allow this in any usable way, that I know.

I advise you to read this small section: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_attachments

You could try retrieving the names from the ActionContext as in this example: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_checking_linked_issues

TAGS
AUG Leaders

Atlassian Community Events