Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

validate number of attached files in issue creation

Pedro J. Acosta September 21, 2020

Hello everyone,

I am trying to create a validation with script that forces the user to introduce at least three attachments in the creation of a request, I have created a custom script validation with the following 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 com.opensymphony.workflow.InvalidInputException
import webwork.action.ActionContext

def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
def attachmentSize = tempWebAttachments.size()

if(attachmentSize < 3) {
throw new InvalidInputException("Necesita adjuntar los siguientes ficheros: Operatoria, Plan de contingencia y Documentación adicional.")
}
}

 

The problem is that it does not work correctly since sometimes it allows me to create requests without adding any attachments or when I enter more than three attachments it skips the validation and does not allow me to create the issue, could you help me with this script please?

 

Regards.

1 answer

1 accepted

1 vote
Answer accepted
Gustavo Félix
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.
September 22, 2020

Hi @Pedro J. Acosta 
I tried your code with a simpled scripted validator, but it didn't run.

Then I tried it with a custom script validator, and it worked fine.

When you go to the transition validators screen, it shows you if your validator ran or not. 
If it is running, you can add a log.warn attachmentSize , just to see what value is logging.

Agustín Ruiz October 6, 2020

Hello Gustavo,

Thank you very much for your help and I am very sorry that it took so long to respond, I have tried both with a simple script and with a custom one and it fails me in both, anyway it is no longer necessary because my client no longer needs this restriction.

 

Regards.

 

P.D.: I'm Pedro, but I made a mistake when writing the comment, sorry for the confusion.

Like Gustavo Félix likes this

Suggest an answer

Log in or Sign up to answer