Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

1 ACCEPTED SOLUTION

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

View solution in original post

5 REPLIES 5

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