The CreatorCon Call for Content is officially open! Get started here.

How to make attachments mandatory with a minimum count (e.g., 2) before submitting a Service Request

iztgokul
Tera Contributor

Hi Everyone,

I have a requirement where users must upload at least 2 attachments before submitting a service (Catalog Item / HR Service).

For example, if a user adds only one attachment, they should not be able to submit the request.

If anyone has done this before, could you please share a sample script or the best approach?

Thanks in advance,
Gokul

3 REPLIES 3

palanikumar
Giga Sage
Giga Sage

Hi @iztgokul 

Try this client script

function onSubmit() {
        var minAttachment = 2;
        var attachmentCount = 0;
	if(window == null){
		// This will run from Service portal
                attachmentCount  = this.document.getElementsByClassName('get-attachment').length;
	}
	else{
		// This will run from native view
                attachmentCount = $j("li.attachment_list_items").find("span").length;
	}
	if(attachmentCount < minAttachment){
		alert('You must add " + minAttachment + " attachments before submitting this request.');
		return false;
	}
}
Thank you,
Palani

Ankur Bawiskar
Tera Patron
Tera Patron

@iztgokul 

this will work in both native + portal for record producer

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length < 3) {
                alert('You must add 3 attachments before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length < 3) {
            alert('You must add 3 attachments before submitting this request.');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Kieran Anson
Kilo Patron

Hi Gokul,

Are the attachments well-defined. For example, they must attach a letter, and a photograph of a document? If so, I would recommend using the attachment variable/question type so the user is guided to what attachments are required. Requiring 2 documents can lead to a user simply uploading the same attachment twice.

https://www.servicenow.com/docs/csh?topicname=attachment.html&version=latest