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 !
Community moderators have prevented the ability to post new answers.
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.
If it's working, I think it's the only way...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Isaac, I think you have another problem. Do the attachments display correctly on the screen? What version are you using?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi... Can you log request.getParameterMap() - see what the keys are.
Is this on the Create action?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
> 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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ok, well done. You just want to "throw new InvalidInputException(...)"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.