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

Hi Ankur Iam feeling it difficulty to do this as this is high level for my experience Can you help me in doing it in my pdi so that I can learn a lot from you My catalog item name is -attachement (transform script) I have shared my pdi credentials to your Gmail Can you please help me if you are free for last time

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

Thanks ankur

asifnoor
Kilo Patron

Hi

Assuming your arr contain the headers of the excel. If yes, then in your code you are checking for == -1 which means it is not matching.

If you want to check for match, then change the condition to >-1

Mark the comment as correct/helfpul if this helps to solve the problem.

find_real_file.png

find_real_file.png

 

When i make an attachment in catalog item if the excel  headers contains these fields mentioned above the attachment should attach in the attachment field if it didnt match then we should get an alert as (please check the excel headers you attached)

I think i should write a onchange client script and use glideajax 

Can You help