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_transition
I'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.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext
import com.opensymphony.workflow.InvalidInputException
def 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Place this code into custom script validator instead of simple script and it will work :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext
import com.opensymphony.workflow.InvalidInputException
def 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")
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You re welcome! If it helps you, please mark answer as accepted. So that, other people will be able to find this answer easily for similar questions :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, I placed the above code into custom script validator and it is not working. Anybody knows why?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamila - if it helps, this worked for me (based on some code found on this discussion
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.opensymphony.workflow.InvalidInputException
def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()
def mIssue = issue as MutableIssue
def attachmentNames = mIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValue
if(!attachmentNames) {
invalidInputException = new InvalidInputException("You must add an attachment")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.