I'm trying to use the following recipe from adaptavist but it just tells gives me an error no matter if I have an attachment or not.https://scriptrunner.adaptavist.com/4.3.4/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html#_validating_attachments_added_this_transitionI'm running JIRA 7.9.0, scriptrunner 5.3.16, and applying it to the "create" transition in my workflow in the validators section.Any ideas whats going wrong? Is the recipe broken? Am I doing something wrong? Is there some customizing of the script I must do for my jira instance/project I'm missing?Appreciate any help.
Hello @Ryot
In scriptrunner example validator just logs filenames
If you want make attachments mandatory, try this code
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManagerimport webwork.action.ActionContextimport com.opensymphony.workflow.InvalidInputExceptiondef temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)if (formToken) { def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) tempWebAttachments.each { TemporaryWebAttachment it -> log.error("Uploaded attachment name: ${it.filename}") } if(!tempWebAttachments){ throw new InvalidInputException(IssueFieldConstants.ATTACHMENT, "You must upload attachment") }}
Here we checks if attachments exist and if not, throw an exception.
Hey @Mark Markov- thanks for the code! this is definitely closer where I need to be, however, it's giving my validator error still (not the one within in your code), and its giving the error regardless if I have an attachment or not.Just to confirm we are on the same page... is this code appropriate for adding as a workflow validator for the create step? I notice that with workflow validators, there is already a dedicated "Error Message" field to enter an error message (and thats the one thats showing up for me)... but I see your code also has an error message within it. Should I be putting this type of code somewhere else, like a listener? Or can it be modded to work as a validator.Again, forgive my ignorance as I'm pretty new to this stuff. I've added a couple screenshots to hopefully clarify.
Place this code into custom script validator instead of simple script and it will work
D'OH. I knew it had to be some kind of small oversight like that.... I kept on editing code from my initial attempt, which was in simple script, so it didn't even occur to me to try the custom script area.
Thanks so much for the help @Mark Markov - everything's working now.For anyone else that stumbles across this I also have restricted it to a specific issue type that we need this on by doing the following...
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManagerimport webwork.action.ActionContextimport com.opensymphony.workflow.InvalidInputExceptiondef temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)if (formToken) { def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) tempWebAttachments.each { TemporaryWebAttachment it -> log.error("Uploaded attachment name: ${it.filename}") } if ((!tempWebAttachments) && issue.issueType.name == "Your Issue Type Name Here"){ throw new InvalidInputException(IssueFieldConstants.ATTACHMENT, "You must upload an attachment") }}
Hi, I placed the above code into custom script validator and it is not working. Anybody knows why?
Same here. This used to work in Jira 7.1.1/Scriptrunner 4.3.16, but doesn't work now we're on Jira 8.5.4/Scriptrunner 5.9.1-p5
Jamila - if it helps, this worked for me (based on some code found on this discussion
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessorimport com.opensymphony.workflow.InvalidInputExceptiondef attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()def mIssue = issue as MutableIssuedef attachmentNames = mIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValueif(!attachmentNames) {invalidInputException = new InvalidInputException("You must add an attachment")}
It looks like you're new here. Sign in or register to get started.