Abort the scheduled data import through Pre-script script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2023 06:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 06:47 AM
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;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 07:05 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 07:40 AM
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;
}
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 08:32 AM
Answer is not the... answer.
Proof - change script to:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 09:22 AM
strange. time to raise HI ticket for this.
Even when answer=true it should execute the import
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
