ScriptRunner - Validation of 2+ attachments on create screen

Michael
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.
August 19, 2024

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

}
But I can't get the validation script to work - it's always stating there are less than 2 attachments - and hence not letting the request be created.
Can someone please help me here?
Thanks,
Mike

2 answers

1 accepted

3 votes
Answer accepted
Michael
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.
August 19, 2024

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

 

0 votes
Hauke Bruno Wollentin
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.
August 19, 2024

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.

Michael
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.
August 19, 2024

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")

}
But I don't know how we could modify this existing code to just check for the number of newly added attachments.
Like Hauke Bruno Wollentin likes this
Hauke Bruno Wollentin
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.
August 19, 2024

Ah, awesome. Learned something new today, thanks for sharing :) 

Suggest an answer

Log in or Sign up to answer