Scheduled Data Export - Pre and Post export script execution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 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
Use a source script on the field mapping within your Export Definition, not a pre/post export script. This lets you transform individual field values before they're written out.
Steps:
- Open your Export Definition → go to the related Columns/Field Maps
- Find (or create) the mapping for
opened_at - Check "Use source script"
- Add your formatting script in the Source script field
Sample script:
// 'source' is the current GlideRecord being exported
var gdt = new GlideDateTime(source.getValue('opened_at'));
if (gdt.isValid()) {
// getValue() returns internal format: yyyy-MM-dd HH:mm:ss
var internal = gdt.getValue();
// Append microsecond precision (ServiceNow stores ms at best)
var ms = gdt.getNumericValue() % 1000;
var micro = ('000000' + (ms * 1000)).slice(-6);
answer = internal.substring(0, 19) + '.' + micro;
} else {
answer = '';
}
// Result: "2025-04-09 14:30:45.123000"
Key points:
- Use
source.getValue('opened_at')to get the UTC internal value (already inyyyy-MM-dd HH:mm:ssformat), avoiding timezone/locale issues fromgetDisplayValue(). - ServiceNow only stores millisecond precision, so the last 3 digits of your microsecond format will always be
000. - The variable
answeris what gets written to the export output — this is the implicit return variable in source scripts.
If you need this for multiple date fields, repeat the same pattern on each column's source script, just changing the field name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi Naveen,
Thanks for the reply but, how can I use a field level script here ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
56m ago
If you don't see a "Use source script" option, your version may not support field-level scripting on export definitions. In that case, use a Transform Map approach as a workaround — or use a pre-export script on the Export Set itself:
Pre-export script (alternative):
// Runs per row. 'current' is the source GlideRecord.
// 'exportRow' is a map of column→value being written out.
var gdt = new GlideDateTime(current.getValue('opened_at'));
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);
exportRow.opened_at = internal.substring(0, 19) + '.' + micro;
}
Note: The
exportRowobject availability depends on your ServiceNow version. If it's not available, test withecc_exportor check your instance's script variables by reviewing the Scheduled Export documentation under your release.
Quick way to confirm what's available: Open the export set record itself and look for a "Script" or "Pre/Post script" field — that's where your row-level logic goes if column-level scripting isn't supported.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
39m ago
are you sure this will work?
The pre and post scripts don't run for each row, they run only once i.e. before the start and after the export ends
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
