How to Make Service Catalog Attachments Mandatory in ServiceNow

Recently I was working with a friend who was trying to make attachments mandatory on a catalog item. All of the code references we could find about how to do this were Server side or client side methods that no longer worked. In this case we needed a client-side solution as the “record” did not exist yet (catalog item hasn’t been ordered) therefore there was no reliable way to search for it (to look for it in the sys_attachment table). Luckily I had client side solution from a buddy here at Crossfuze as well, and after some tweaking we were able to get it working in Geneva/Helsinki.

Without further waiting, here’s what it looks like. Please note that it is using the DOM, so you will want to add it to your regression tests for new upgrades to ensure it keeps working.

function onSubmit() {
	//when type = file_needed, ensure an attachment was added
	
	function checkAttachments() {
		var display = $('header_attachment_list_label').getStyle('display');
		if (display == 'hidden' || display == 'none' ) {
			jslog('Attachment Not Found. Display value: ' + display);
			return false;
		} else {
			jslog('Found Attachment. Display value was: ' + display);
			return true;
		}
	}
	
	if(g_form.getValue('type') == 'file_needed' && !checkAttachments()){
		alert('You must add an attachment prior to submitting.');
		jslog(' You must add an attachment before submitting.');
		return false;
	}
	
}

2 thoughts on “How to Make Service Catalog Attachments Mandatory in ServiceNow

    • jmaxwell says:

      Good catch; you are correct; my parser must have escaped the characters. It should be “&&”, I have now updated the code to match.

Leave a Reply

Your email address will not be published. Required fields are marked *