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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So you are searching for all the headers

you can get all the headers like this using sample script

var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', '62ce7a65070d1c90540bf2508c1ed0c7');
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();

gs.info('Columns are'+headers);

Regards
Ankur

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

Hi Ankur I need to validate the excel headers in catalog item if the excel headers are having (number,name, assignment group,short description) the attachement should insert to catalog item otherwise should throw an error

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

Hi,

for this you would require onSubmit Client Script with GlideAjax

You can use synchronous approach shared in this link

How to achieve getXMLWait() functionality on Service Portal

Regards
Ankur

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