Service Catalog item in Service Now - Make attachment mandatory

Monday Jan 1, 0001

Here’s an onSubmit function to make an attachment mandatory in a Service Now Service Catalog item.

Code

Here’s the code:

function onSubmit() {
	var cat_id = g_form.getParameter('sysparm_attachment_cart_id');   
	var attach = new GlideRecord('sys_attachment');
	var hasAttachment = false;
	
	attach.addQuery('table_sys_id',cat_id);
	attach.query();
	if(attach.next()){
		hasAttachment = true;
	} 
	
	var shouldHaveAttachment = g_form.getValue("use_spreadsheet") === "true";
	if(shouldHaveAttachment === true && !hasAttachment){
		g_form.addErrorMessage("You should have attached a file!");
		return false;
	}
	
	return true;
}
 

Note that the g_form.addErrorMessage is optional, of course…