Avoid sending empty file in Scheduled Data Exports
This feature also allows to send Delta Exports. Enabling the delta export allows only new or changed records to get exported from the last export run time.
Issue: ServiceNow out of the box doesn't refrain from sending the empty file to the remote location if there is no data associated to it.
Approach: The field "Last export scheduled run" present on Scheduled Data Export can be leveraged. This fields gets automatically populated with the last export run time when delta exports is enabled. If you don't have any updated/created records after this time you can avoid sending empty file.
1) Determine the value for "Last export scheduled run" for the current Scheduled Data Export
2) Retrieve the date and time separately from the above date/time value
3) Enable the conditional checkbox: Select this check box to run the schedule only when a condition is met.
4) In the condition script perform this: When the condition script evaluates to false the scheduled export doesn't run
Use the date and time value in forming the encoded query on the Export Definition table with condition as Updated at or after the above time
a) if records found then set answer = true
b) if no records found then set answer = false
Script: Using GlideRecord
var lastRunTime = '';
// give sys_id of the current scheduled data export
var scheduledExportRec = new GlideRecord("scheduled_data_export");
scheduledExportRec.get("yourSysId");
var lastRunTime = scheduledExportRec.last_export_run;
// retrieve the date and time separately
var date = lastRunTime.split(" ")[0];
date = "'" + date + "'";
var time = lastRunTime.split(" ")[1];
time = "'" + time + "'";
// form the encoded query
var encodedQuery = 'sys_updated_on>=javascript:gs.dateGenerate(' + date + ',' + time + ')';
// query the export definition table with this encoded query
// give the table name of your export definition
var tableObj = new GlideRecord("tableName");
tableObj.addEncodedQuery(encodedQuery);
tableObj.query();
answer = tableObj.hasNext();
Script Using GlideAggregate: This approach would be beneficial when the export definition table has huge amount of data.
var lastRunTime = '';
// give sys_id of the current scheduled data export
var scheduledExportRec = new GlideRecord("scheduled_data_export");
scheduledExportRec.get("yourSysId");
var lastRunTime = scheduledExportRec.last_export_run;
// retrieve the date and time separately
var date = lastRunTime.split(" ")[0];
date = "'" + date + "'";
var time = lastRunTime.split(" ")[1];
time = "'" + time + "'";
// form the encoded query
var encodedQuery = 'sys_updated_on>=javascript:gs.dateGenerate(' + date + ',' + time + ')';
// query the export definition table with this encoded query
// give the table name of your export definition
var count = 0;
var tableObj = new GlideAggregate("tableName");
tableObj.addEncodedQuery(encodedQuery);
tableObj.addAggregate('COUNT');
tableObj.query();
if(tableObj.next())
count = tableObj.getAggregate('COUNT');
if(count > 0)
answer = true;
else
answer = false;
Screenshots:
In my example the export definition is present on Incident table and hence I am using incident table during the query; You can use yours
Thanks for reading the blog and do provide your inputs/suggestions if any.
Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.Thanks,
Ankur Bawiskar
Labels:
https://www.servicenow.com/community/developer-blog/avoid-sending-empty-file-in-scheduled-data-exports/ba-p/2286855