Scheduled Data Export - Pre and Post export script execution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi All,
I want to export incident table data to MID Server and I have created a export set and definition for that. I have some date/time fields for ex, "opened_at". I want to change the format in the excel sheet to "yyyy-mm-dd HH:mm:ss.SSSSSS".
Could anyone please suggest if, I have to use a pre or post script ? And what and how to write the logic. Any sample script could help a lot.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Tried the script in the pre script but, it seems not to work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
that's not possible to manipulate the date/time format.
Please inform this to your customer
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Thank you for marking my response as helpful.
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
This should work definitely
Scheduled Script — full control over date formatting and MID Server delivery:
var gr = new GlideRecord('incident');
gr.addQuery('active', true); // adjust your filters
gr.query();
var rows = [];
while (gr.next()) {
var gdt = new GlideDateTime(gr.getValue('opened_at'));
var formatted = '';
if (gdt.isValid()) {
var internal = gdt.getValue(); // yyyy-MM-dd HH:mm:ss
var ms = gdt.getNumericValue() % 1000;
var micro = ('000000' + (ms * 1000)).slice(-6);
formatted = internal.substring(0, 19) + '.' + micro;
}
rows.push({
number: gr.getValue('number'),
short_description: gr.getValue('short_description'),
opened_at: formatted
// add more fields as needed
});
}
// Build CSV
var csv = 'number,short_description,opened_at\n';
for (var i = 0; i < rows.length; i++) {
var sd = rows[i].short_description || '';
csv += '"' + rows[i].number + '","' +
sd.replace(/"/g, '""') + '","' +
rows[i].opened_at + '"\n';
}
// Push to MID Server via ECC Queue
var ecc = new GlideRecord('ecc_queue');
ecc.initialize();
ecc.setValue('agent', 'mid.server.YOUR_MID_SERVER_NAME');
ecc.setValue('topic', 'SystemCommandProcessor');
ecc.setValue('name', 'incident_export_' + new GlideDateTime().getValue());
ecc.setValue('source', 'incident_export');
ecc.setValue('payload', csv);
ecc.insert();
gs.info('Export complete. Rows exported: ' + rows.length);
Setup steps:
- Navigate to System Definition → Scheduled Jobs → Scheduled Script Executions
- Create a new record, paste the script above
- Replace
YOUR_MID_SERVER_NAMEwith your actual MID Server name - Adjust the
addQueryfilters and field list to match your requirements - Set the schedule (or run on demand)
