Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

1 ACCEPTED SOLUTION

AmanPratapS7233
Tera Contributor

Hi All, I got the solution - we just have to go to the 'import export' system property and in the 'export csv format' field, we have to write UTF-8 instead of default and this works fine then.

View solution in original post

14 REPLIES 14

Tried the script in the pre script but, it seems not to work.

@AmanPratapS7233 

As mentioned in my earlier comment to @Naveen20 that's not going to work

@Naveen20 If you have a working approach with screenshots supporting your above comment, please do share that

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

Ankur Bawiskar
Tera Patron

@AmanPratapS7233 

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! 🙏

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

@AmanPratapS7233 

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! 🙏

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

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:

  1. Navigate to System Definition → Scheduled Jobs → Scheduled Script Executions
  2. Create a new record, paste the script above
  3. Replace YOUR_MID_SERVER_NAME with your actual MID Server name
  4. Adjust the addQuery filters and field list to match your requirements
  5. Set the schedule (or run on demand)