Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Log Records in Import Log When Using Choice/Reference Fields with Reject in Transform map

Med99
Tera Contributor

Hello,

In a Transform Map, some fields are configured with the "Reject" action when a reference or choice value is invalid.

As a result, the record is skipped, and an error message appears in sys_import_set_row.comment, but no entry is created in import_log.

However, our internal reporting relies solely on the _import_log table , so these rejections are completely invisible in our logs.

Is there a (preferably standard) way to force a log entry in import_log when a value is rejected (choice/ref field)? Ideally without introducing instability or interfering with the import process.

 

I’ve already tried the following:

  • An onAfter transform script to read skipped rows from sys_import_set_row and create logs in import_log, but it doesn’t work because these rows are only created after the transform is fully complete so they can’t be captured reliably. It also causes the import to remain stuck in a "Loaded" state. also onAfter only executes after a record has been transformed and inserted/updated which does not happen for ignored/skipped rows.

  • A Business Rule to copy the error to import_log, but ServiceNow prevents setting the import_set field (which links the log to the import set) for security reasons. This field is only auto-populated when the error comes directly from the Transform Map itself

Thank you in advance for your help! 

Med99_0-1762347018314.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Med99 

how about using onComplete transform script

something like this, this is just sample but please enhance

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here

    // Get current import set sys_id
    var importSetSysId = import_set.sys_id; // current is the import set record

    // Query rejected rows in sys_import_set_row for this import set
    var rejectedRows = new GlideRecord('sys_import_set_row');
    rejectedRows.addQuery('import_set', importSetSysId);
    rejectedRows.addQuery('state', 'skipped'); // or 'error', depending on status
    rejectedRows.query();

    while (rejectedRows.next()) {
        // Build the import_log record
        var log = new GlideRecord('import_log');
        log.initialize();
        log.import_set = importSetSysId; // link to import set
        log.message = 'Row ' + rejectedRows.sys_import_row + ' rejected: ' + rejectedRows.sys_import_state_comment;
        log.insert();
    }
})(source, map, log, target);

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Med99 

how about using onComplete transform script

something like this, this is just sample but please enhance

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here

    // Get current import set sys_id
    var importSetSysId = import_set.sys_id; // current is the import set record

    // Query rejected rows in sys_import_set_row for this import set
    var rejectedRows = new GlideRecord('sys_import_set_row');
    rejectedRows.addQuery('import_set', importSetSysId);
    rejectedRows.addQuery('state', 'skipped'); // or 'error', depending on status
    rejectedRows.query();

    while (rejectedRows.next()) {
        // Build the import_log record
        var log = new GlideRecord('import_log');
        log.initialize();
        log.import_set = importSetSysId; // link to import set
        log.message = 'Row ' + rejectedRows.sys_import_row + ' rejected: ' + rejectedRows.sys_import_state_comment;
        log.insert();
    }
})(source, map, log, target);

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader