ScriptRunner - Validation of 2+ attachments on create screen
Asked by Michael:

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


Answers:
author: Michael
body: 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.InvalidInputExceptionimport com.atlassian.jira.component.ComponentAccessordef am = ComponentAccessor.getAttachmentManager()def cfm = ComponentAccessor.getCustomFieldManager()def requestTypeField = cfm.getCustomFieldObject("customfield_#######")def requestTypeValue = issue.getCustomFieldValue(requestTypeField)?.toString()// Get all attachments for the issuedef 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 metreturn true



author: Hauke Bruno Wollentin
body: 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.

comments: author: Michael
body: Hello [@Hauke Bruno Wollentin](/t5/user/viewprofilepage/user-id/5238429)

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.


author: Hauke Bruno Wollentin
body: Ah, awesome. Learned something new today, thanks for sharing :)


Tags: data-center

groovy

issue-create

jira-data-center

script

scriptrunner

validation

validator

workflow