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

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

@iztgokul 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct ✔️ and close the thread 🔒 — this helps future readers find the solution faster! 🙏

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

@iztgokul 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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