Hello everyone,
I'm try to create a "simple validation script" using ScriptRunner which validates that 2+ attachments have been added to a creation screen IF the customfield "request type" = "Urgent Vendor Payment".
So far I have this code:
import com.atlassian.jira.component.ComponentAccessor
def am = ComponentAccessor.getAttachmentManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def requestTypeField = cfm.getCustomFieldObject("customfield_12901")
def requestTypeValue = issue.getCustomFieldValue(requestTypeField).toString()
if (requestTypeValue == "Urgent Vendor Payment") {
ComponentAccessor.attachmentManager.getAttachments(issue).size() >= 2
}
We figured it out based on our previous script. The key was using "issue.getAllAttachments()" this works with ".size()" in order to validate whether or not there are 2 attachements within the create window.
See below for the working code: (I've "#####" the request type value and the customfield value)
import com.opensymphony.workflow.InvalidInputException import com.atlassian.jira.component.ComponentAccessor def am = ComponentAccessor.getAttachmentManager() def cfm = ComponentAccessor.getCustomFieldManager() def requestTypeField = cfm.getCustomFieldObject("customfield_#######") def requestTypeValue = issue.getCustomFieldValue(requestTypeField)?.toString() // Get all attachments for the issue def attachments = issue.getAllAttachments() if (requestTypeValue == "########" && attachments.size() < 2) { throw new InvalidInputException("attachment", "You must upload at least 2 attachments for Urgent Vendor Payment") } // Default return true if the condition is not met return true
AFAIK you can't fetch "realtime" data - like the amount of attachment in the screen the user is _currently_ viewing in a workflow validation.
To tackle this, SR Behaviours should work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Hauke Bruno Wollentin
I'm thinking you can - since there is an example script for scriptrunner validators:
import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.attachmentManager.getAttachments(issue).size() >= 1
But I'm wondering if I need to do something different since the screen I'm trying to validate on is the "Create" screen.
We already have a create screen validator for specific types of newly attached files that works here:
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
def am = ComponentAccessor.getAttachmentManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def hasCsvAttachment = issue.getAllAttachments().any{ it.filename.endsWith(".csv") }
def requestTypeField = cfm.getCustomFieldObject("customfield_17800")
def requestTypeValue = issue.getCustomFieldValue(requestTypeField).toString()
if (requestTypeValue == "Option1" && !hasCsvAttachment) {
throw new InvalidInputException("attachment", "You must have a csv attachment")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah, awesome. Learned something new today, thanks for sharing :)
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.