How to check attachment before on submit client script in catalog item & attachment must be xlsx

surajsironi
Kilo Sage

Hi All,

Kindly help me 
How to check attachment before on submit client script in catalog item &
there must be 1 attachment and that attachment type must be xlsx only.


2 REPLIES 2

Brad Bowman
Kilo Patron

This won't work in Service Portal, and the Isolate script box must be unchecked.  These scripts are dynamic so they can be used in different scenarios - attached to various tables, types, file names, limits etc.

function onSubmit() {
	var itemsysid = g_form.getValue('sysparm_item_guid');
	if(!itemsysid){
	    itemsysid = gel('sysparm_item_guid').value;
	}
	var ga = new GlideAjax('CheckAttachment');
	ga.addParam('sysparm_name', 'isAttached');
	ga.addParam('sysparm_sysid', itemsysid);
	ga.addParam('sysparm_tablename', 'sc_cart_item');
	ga.addParam('sysparm_fileoperator', 'ENDSWITH');
	ga.addParam('sysparm_filepattern', '.xlsx');
	ga.getXMLWait();

	var answer = ga.getAnswer();
	answer = answer.evalJSON(); //Transform the JSON string to an object

	if (answer.attached == 'false') {
		alert("You must attach the Excel file.");
		return false;
	}
        if (answer.count > 1) {
		alert("Only one file can be attached.");
		return false;
	}
}
var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isAttached: function() {
		var obj = {};  
		obj.attached = 'false';  
		obj.count = 0;  
		var id = this.getParameter('sysparm_sysid');
		var tablename = this.getParameter('sysparm_tablename');   //'sc_cart_item'
		var fileoperator = this.getParameter('sysparm_fileoperator');   //'ENDSWITH'
		var filepattern = this.getParameter('sysparm_filepattern');    //'.xlsx'
		var attachment = new GlideRecord('sys_attachment');
		attachment.addQuery('table_name', tablename);
		if(filepattern){
			attachment.addQuery('file_name', fileoperator, filepattern);
		}
		attachment.addQuery('table_sys_id',id);
		attachment.query();

		while (attachment.next()) {
			obj.count++;
			obj.attached = 'true';
		}
		var json = new JSON();
		var data = json.encode(obj);//JSON formatted string  

		return data;
	},
	
	type: 'CheckAttachment'
});

 

Hi @Brad Bowman  ,

Thanks for your reply,

can we use getXMLAnswer() rather than using getXMLWait().

ga.getXMLWait();