Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

validate the excel parser in catalog item

shashank32
Giga Contributor

the condition is when a user attaches the attachment in catalog item   if the excell headers didnt matched with {number,name,assigned to ,assignment group) the attachment should not attach to the catalog item   

by using Glide Ajax in catalog item i trie this 

if  the attachment headers  matched  the below condition 

find_real_file.png

 

   if it matches then only the attachment  should be created in sc_req_item

1 ACCEPTED SOLUTION

Hi,

Please find the scripts below

I just tested with 1 excel file containing the name header and other which didn't contain the name header

You can enhance it as per your need

function onSubmit(){

	var cat_id = g_form.getValue('sysparm_item_guid');
	var ga = new GlideAjax('validateheaders');
	ga.addParam('sysparm_name', 'validate');
	ga.addParam('sysparm_item_guid', cat_id);
	ga.getXMLWait();
	var answer = ga.getAnswer();
	if(answer == 'false'){
		g_form.addErrorMessage('not valid headers');
		return false;
	}
}

Script Include:

var validateheaders = Class.create();
validateheaders.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	validate: function() {
		var attachID;
		var test = this.getParameter('sysparm_item_guid');
		var gr = new GlideRecord('sys_attachment');
		gr.addQuery('table_sys_id', test);
		gr.addQuery('table_name','ZZ_YYsc_cart_item');
		gr.query();
		if (gr.next()) {
			attachID = gr.getUniqueValue();
		}
		var stream = new GlideSysAttachment().getContentStream(attachID);
		var parser = new sn_impex.GlideExcelParser();
		parser.parse(stream);
		var headers = parser.getColumnHeaders();
		var arr = headers.toString().split(',');

		if(arr.indexOf('Name') == -1)
			return false;

		/*if ((arr.indexOf('Name') == -1) || (arr.indexOf('Number') == -1) || (arr.indexOf('assigned to') == -1) || (arr.indexOf('assignment group') == -1) || (arr.indexOf('short description') == -1)) {
			return 'false';
		}
		*/
		return 'true';
	},

	type: 'validateheaders'
});

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

OnChange will not work on file upload.

write onBefore BR insert on sys_attachment table

Table name is sc_req_item

then in script, you do the check and add

gs.addErrorMessage("Kindly check the headers");

current.setAbortAction(true)

Mark the comment as a correct answer and helpful if this helps.

Hi But I need to populate the error message to end user in catalog form page Can this code works?