Scheduled Data Export - Pre and Post export script execution

AmanPratapS7233
Tera Contributor

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

10 REPLIES 10

Naveen20
ServiceNow Employee

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:

  1. Open your Export Definition → go to the related Columns/Field Maps
  2. Find (or create) the mapping for opened_at
  3. Check "Use source script"
  4. 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 in yyyy-MM-dd HH:mm:ss format), avoiding timezone/locale issues from getDisplayValue().
  • ServiceNow only stores millisecond precision, so the last 3 digits of your microsecond format will always be 000.
  • The variable answer is 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.

Hi Naveen, 

 

Thanks for the reply but, how can I use a field level script here ?

AmanPratapS7233_0-1775735159205.png

 

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):

 
 
javascript
// 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 exportRow object availability depends on your ServiceNow version. If it's not available, test with ecc_export or 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.

Screenshot 2026-04-09 at 5.26.06 PM.png

@Naveen20 

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

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader