Verify if an attachment is pdf or not before submission [Service Portal]

Ashish Gupta6
Tera Contributor

Verify if an attachment is pdf or not before submission!!

we want to verify if the attachment attached by an end user is pdf or not.

if pdf --> allow submission

if non-pdf --> alert message to attach a pdf.

8 REPLIES 8

Shambhu5
Kilo Guru

Hi Ashish,

I had similar issue in one of my Projects, where I had to Verify whether attached Attachment type is of .pdf/.txt.

I made use of below catalog client script onSubmit and that worked for me. Please find below code:

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
		
		
	}
}

Please mark my answer as Correct/Helpful in case that helped.

Regards,
Vishrut

Above doesn't work in the portal since the table of the attachment ends up defined on a portal table and then is moved when the real record is created. 

Before moving on here. Question will be how you want to solve this. Through a custom widget or client script. Which way kind of depends on the requirements and if it's only for a specific item or majority etc.

//Göran

and please don't do GlideRecord calls within a Client script.

 

//göran

Hi @Goran WitchDoc 

gel('sysparm_item_guid').value; It's not available in the system.

Can you please give me the solution?

 

Thanks