Making an attachment mandatory in a catalog item

SK41
Giga Guru

Hi,

 

I have a requirement to make the attachment mandatory on the catalog item. User does not use service portal, so I cannot use "mandatory checkbox" in the catalog. My requirement is to check if the user has attached the file before submitting the catalog. If not, show them error message "Please attach a file".

 

How can I achieve that?

Thanks!

1 ACCEPTED SOLUTION

Hi Anurag,

 

I used below code in onSubmit catalog client script and it worked for me:

 

var count = getCurrentAttachmentNumber();
    if(count == 0){
        g_form.addErrorMessage('You must add an attachment');
        return false;
    }

 

View solution in original post

17 REPLIES 17

udaysingh16
Tera Contributor

For Desktop:

function onSubmit() {
			var cat_id = g_form.getParameter("sysparm_item_guid");//This will return the sc_cart_item sys_id that is reserved. 
			var gr = new GlideRecord("sys_attachment");
			gr.addQuery("table_name", "sc_cart_item"); //unnecessary, but improves query efficiency
			gr.addQuery("table_sys_id", cat_id);
			gr.query(); //synchronous for non-service portal CMS.
			if (!gr.hasNext()) {
                                       g_form.addErrorMessage("Please Attach a File");
                                       return false;
			}
		}
	}
}

For Mobile/Service Portal: 

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var condition= g_form.getValue('condition');
	
	if(condition=='upload_file')
		{

			if (this.document.getElementsByClassName('get-attachment').length == 0)	{
				 g_form.addErrorMessage('Please Attach a file');return false;
			}
		}
}

You should not use glide record in a client script.

-Anurag

Just tested this (onsubmit catalog client script) and it works 

 

var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach the completed form before submitting this request.');
return false;

 

-Anurag