Script to Run on catalog item to Count and Limit Attachment Number

satya30
Tera Contributor

HI @Ankur Bawiskar 

I need to limit the number of attachments to 1  on the catalog item while ordering 

i have checked yours previous answer available here Limit number of attachements on catalog Item - Developer Community - Question - ServiceNow Community but it not working.

i written some code to find attachment is attached or not, but how to add filter to count the number of attachments

20 REPLIES 20

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I have written blog on this.

It should work in both native + portal

Please mark blog as helpful and also bookmark

Verify Mandatory Attachments Count on Catalog Item

Adding here for your reference

For example: You need to ensure there are exact 1 attachment added before submission

Script: onSubmit

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

Isolate Script: Set to False

function onSubmit() {

	//Type appropriate comment here, and begin script below
	var countRequired = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must add ' + countRequired + ' 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 ' + countRequired + ' attachments before submitting this request.');
			return false;
		}
	}

}

find_real_file.png

Regards
Ankur

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

Thank you @Ankur Bawiskar 

can a particular type of attachments

here in this case i need to add only excel files

Hi,

If my response helped please mark response as correct and helpful to close the question.

Also mark the blog as helpful and also bookmark it

Regards
Ankur

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

Hi,

yes that is also possible but would require some DOM manipulation

Regards
Ankur

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