Ankur Bawiskar
Tera Patron
Tera Patron

Problem Statement: Need to check if exact 'n' number of attachments are added or not when user submit a request through service catalog from Portal/Native view.

I know, there are couple of questions regarding making number of attachments mandatory on Native UI and Service Portal; so thought of adding this in blog..

The below script would help achieving the same.

For example: You need to ensure there are exact 2 attachments added before submission

Script: onSubmit

Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works

function onSubmit() {
	//Type appropriate comment here, and begin script below

var countRequired = 2;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must add 2 attachments before submitting this request.');
			return false;
		}
	}
	else{
		// native view
         var length = $j("li.attachment_list_items").find("span").length;
		if(length != countRequired){
			alert('You must add 2 attachments before submitting this request.');
			return false;
		}
	}
}

Isolate Script: Set to False

find_real_file.png

Native View:

1) When 0 attachment added; alert shown to user and stop submission

find_real_file.png

2) When 1 attachment is added; alert shown to user and stop submission

find_real_file.png

3) When exact 2 attachments are added; alert not shown and allowed submission

find_real_file.png

4) When more than 2 attachments are added; alert shown to user and stop submission

find_real_file.png

Portal View:

1) When 0 attachment added; alert shown to user and stop submission

find_real_file.png

2) When 1 attachment is added; alert shown to user and stop submission

find_real_file.png

3) When exact 2 attachments are added; alert not shown and allowed submission

find_real_file.png

4) When more than 2 attachments are added; alert shown to user and stop submission

find_real_file.png

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.
Thanks,
Ankur Bawiskar

ServiceNow MVP 2020,2019,2018

My Articles & Blogs

25 Comments