logo

NJP

Avoid sending empty file in Scheduled Data Exports

Import · Apr 05, 2020 · article

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.

https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/export-sets/task/...

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

image

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

ServiceNow MVP 2020,2019,2018

My Articles & Blogs

Labels:

View original source

https://www.servicenow.com/community/developer-blog/avoid-sending-empty-file-in-scheduled-data-exports/ba-p/2286855