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.

Abort the scheduled data import through Pre-script script.

ankit19
Tera Contributor

I need to abort the scheduled data import from Excel, based on checking headers of imported Excel sheet.

From Below script I'm able to get the headers name in system logs but cannot find a funtion which I can call inside the if(header1 != 'Allocation Logic') to abort the import submission.

 

ankit19_1-1683121805024.png

 

 

25 REPLIES 25

Ankur Bawiskar
Tera Patron
Tera Patron

@ankit19 

check that part in the condition field.

1) Enable conditional checkbox

2) add your script in condition field

3) set answer= false; to stop the import from running

Something like this

answer = checkCondition();

function checkCondition(){

	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_sys_id",'f7ed9ac61b84555041abdb12f54bcb76');
	gr.query();
	if (gr.next()) {

		var parser = new sn_impex.GlideExcelParser();
		var attachment = new GlideSysAttachment();
		var attachmentStream = attachment.getContentStream(gr.sys_id.toString());
		parser.parse(attachmentStream);
		var headers = parser.getColumnHeaders();
		// gs.addInfoMessage("ca" +headers.toString());
		var header1 = headers[0];
		// gs.info("headers from sheet" + headers.toString());
		// gs.info("headers from sheet header1" + header1);
		if(header1 == 'Allocation Logic'){
			gs.info("headers from sheet header2" + header1);
			return false;
		}
		else{
			return true;
		}
	}

}

AnkurBawiskar_0-1683208028540.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar thanks for your response.

I have followed the steps provided above, added the script and recieved the logs msg too.

But cannot able to restrict the import.

Only step I cannot able to do is 3rd,  set answer= false; to stop the import from running.

Can you please suggest, where to add this step.

@ankit19 

try this

answer = true;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id",'f7ed9ac61b84555041abdb12f54bcb76');
gr.query();
if (gr.next()) {

	var parser = new sn_impex.GlideExcelParser();
	var attachment = new GlideSysAttachment();
	var attachmentStream = attachment.getContentStream(gr.sys_id.toString());
	parser.parse(attachmentStream);
	var headers = parser.getColumnHeaders();
	// gs.addInfoMessage("ca" +headers.toString());
	var header1 = headers[0];
	// gs.info("headers from sheet" + headers.toString());
	// gs.info("headers from sheet header1" + header1);
	if(header1 == 'Allocation Logic'){
		gs.info("headers from sheet header2" + header1);
		answer = false;
	}
}

If this doesn't work then use this in Pre-export script

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id",'f7ed9ac61b84555041abdb12f54bcb76');
gr.query();
if (gr.next()) {

	var parser = new sn_impex.GlideExcelParser();
	var attachment = new GlideSysAttachment();
	var attachmentStream = attachment.getContentStream(gr.sys_id.toString());
	parser.parse(attachmentStream);
	var headers = parser.getColumnHeaders();
	// gs.addInfoMessage("ca" +headers.toString());
	var header1 = headers[0];
	// gs.info("headers from sheet" + headers.toString());
	// gs.info("headers from sheet header1" + header1);
	if(header1 == 'Allocation Logic'){
		gs.info("headers from sheet header2" + header1);
		cancel = true;
	}
}
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Answer is not the... answer.
Proof - change script to:

2023-05-04_3.png

 

No matter how many times I press "Execute Now", no rows are added to the import set row table.

Because even though variable answer is set to true, the last statement evaluates to undefined, which is not true and that is in fact considered.

 

@-O- 

strange. time to raise HI ticket for this.

Even when answer=true it should execute the import

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