How to allow only 5 attachments and restrict to pdf format in service catalog?

Bharath
Tera Contributor

I have a catalog items. --> Need to restrict the user to attach only 5 attachments and all the attachments has to be in pdf format only.

 

Please suggest how to achieve this.

5 REPLIES 5

BharathChintala
Mega Sage

  

 

@Bharath

Script: onSubmit

Order: 100

Note: GlideRecord is not recommended in Client scripts. but you don't have other way to check PDF format

function onSubmit()  {
	//Type appropriate comment here, and begin script below
	
	var cat_id = gel('sysparm_item_guid').value;
	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_name", "sc_cart_item");
	gr.addQuery("table_sys_id", cat_id);
	gr.query();
	if (!gr.next()) {
			
		alert("You must attach a file to submit.");
		return false;
			
	}
	
	else {
		
		var r = gr.getValue('file_name'); //file_name is a field on sys_attachment table which stores MIME types of attached file.
		//alert('date'+r);
		if(r.indexOf('txt') != -1 ) {
			
			//	alert ('Thanks for udate');
		} else if(r.indexOf('pdf') != -1 ) {
			
			//	alert ('Thanks for udate');
		} else {
			
			alert ('Invalid Insert. Please remove the attachment and attach .txt and .pdf format files only');
			gr.setAbortAction(false);
			gr.getValue('file_name').clearOptions('file_name');
			return false;
		}
		
		//Type appropriate comment here, and begin script below
		
		
	}
}

Script: onSubmit

Order: 200

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 = 5;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length > countRequired) {
			alert('You can't add more than 5 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 can't add more than 5 attachments before submitting this request.');
			return false;
		}
	}
}

 

 

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Hi @BharathChintala ,

 

 

I am getting error - "gel is not defined".

MY catalog client script is in Scoped Application.

 

@Bharath  did you check isolate script check box to false?

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Hi @BharathChintala ,

 

var cat_id = g_form.getValue('sysparm_item_guid'); 

 

This script is working in Native view but its not working on portal.

 

Can you pls suggest is there anything that needs to be done in portal