How to make attachment mandatory?

Swarna Radha August 22, 2018

Hi, I have used Field(s) changed validator "attachment" but it is not making the field mandatory.

Then I have used script validator using condition below:

import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.attachmentManager.getAttachments(issue).size() = 0

 

This is not letting me submitting the form as the error message is displaying.

Is there any solution?

 

Thanks

Swarna

2 answers

1 accepted

8 votes
Answer accepted
Mark Markov
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 22, 2018

Hello @Swarna Radha

You can create custom script validator to achieve this.

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")
}
}
Swarna Radha August 22, 2018

Hi Mark 

It is working :)

Thanks

Swarna

Kristján Geir Mathiesen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 23, 2020

Thank you @Mark Markov  Works very nice. Very nice, indeed.

viswa.reddy June 8, 2022

Hi Mark,

Is there a way to make attachments filed mandatory based on custom field value?

for example, I want to make attachments mandatory only when field value = x

we are using jira server with script runner plugin.

Like Zakhar likes this
Zakhar September 1, 2022

Hi @viswa.reddy, Do you have the solution for your case? I need exactly the same. Thank you! 

Prathima January 31, 2024

Did you get solution for this case ?

2 votes
Mohamed Adel
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 22, 2018

Hello @Swarna Radha

 

Please use the following instead with simple script validator: 

import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.attachmentManager.getAttachments(issue).size() >= 1

I hope this will help

siva February 16, 2020

@Mohamed Adel 

 

Any option to check if the no of attachments are 4 in number.

 

and for a specific transition.

Michael Kornatzki
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.
November 16, 2020

The solution for the SimpelScriptValidator ends every time in a NullPointerException.

The solution above for the custom script validator works.

Sascha Ziemann December 10, 2020

This checks, if a issue has something attached. But during the transition the attachment is not yet been attached. This is the reason why the TemporaryWebAttachmentManager has to be used.

Suggest an answer

Log in or Sign up to answer