"Error occurred during row insert" while importing JSON data using two transform maps

beycos
Tera Contributor

Hi everyone,

I'm facing an issue while importing data from a JSON file using a Data Source in ServiceNow. There are two transform maps involved in the process:

  • 1 – uses a field map, and the name field has COALESCE = true

  • 2 – a script-only transform map

Use case:

I'm trying to import two records that have the same name — one active, and one that was previously deleted . The script in 2 is designed to allow this and seems to handle the logic correctly.

Issue:

Despite the script seemingly working, I consistently receive the following error during the import:

"Error occurred during row insert"

This is my script

(function runTransformScript(source, map, log, target) {
var recordName = source.record_name; 
var deletionDate = source.date_deleted; 
var updated = false;

// 1. Find record with same name AND deletion date
var existing = new GlideRecord('my_table_name'); 
existing.addQuery('name', recordName);
existing.addQuery('date_deleted', deletionDate);
existing.query();

if (existing.next()) {
log.info('Found matching record, updating: ' + existing.sys_id);

existing.status = source.status;
existing.company = source.company;
existing.code = source.code;
existing.description = source.description;
// Update other fields as needed

existing.update();
updated = true;
}

// 2. If not found, find active record (no deletion date) with same name
if (!updated) {
var active = new GlideRecord('my_table_name');
active.addQuery('name', recordName);
active.addQuery('date_deleted', ''); // Active records only (no deletion date)
active.query();

if (active.next()) {
log.info('Found active record with same name, updating: ' + active.sys_id);

active.status = source.status;
active.company = source.company;
active.code = source.code;
active.description = source.description;
active.date_deleted = source.date_deleted; // Mark as deleted if applicable

active.update();
updated = true;
}
}

// 3. If still not updated, insert new record
if (!updated) {
var newRec = new GlideRecord('my_table_name');
newRec.initialize();

newRec.name = recordName;
newRec.status = source.status;
newRec.company = source.company;
newRec.code = source.code;
newRec.description = source.description;
newRec.date_deleted = source.date_deleted;

var newSysId = newRec.insert();
if (newSysId) {
log.info('Inserted new record: ' + newSysId);
}
}

// Prevent default insert/update by transform map
target.setAbortAction(true);
})(source, map, log, target);

 

Could you please help me understand why this error occurs, especially since the script is allowing the insertion? 

Thanks in advance for your help!

5 REPLIES 5

  1.  Converted Map 2 to Script-Only
    • Removed all field maps
    • Ensured all insert/update logic was handled via GlideRecord
    • Declared target.setAbortAction(true) early in the script
  1. Disabled Map 1
    • Since Map 2 handles all logic, I disabled Map 1 to prevent coalesce conflicts
  2.  Use onBefore Script in Map 1 (if needed)

if (action == "insert") {

  ignore = true;

}

  1. Validated Permissions and Field Values

Confirmed ACLs, reference integrity, and choice values were correct

  1. Wrapped Insert in Try-Catch

Added logging to catch any hidden exceptions and confirm successful inserts

(function runTransformScript(source, map, log, target) {

    target.setAbortAction(true); // Prevent default insert/update

 

    var recordName = source.record_name;

    var deletionDate = source.date_deleted;

    var updated = false;

 

    try {

        var existing = new GlideRecord('my_table_name');

        existing.addQuery('name', recordName);

        existing.addQuery('date_deleted', deletionDate);

        existing.query();

        if (existing.next()) {

            existing.status = source.status;

            existing.company = source.company;

            existing.code = source.code;

            existing.description = source.description;

            existing.update();

            updated = true;

        }

 

        if (!updated) {

            var active = new GlideRecord('my_table_name');

            active.addQuery('name', recordName);

            active.addQuery('date_deleted', '');

            active.query();

            if (active.next()) {

                active.status = source.status;

                active.company = source.company;

                active.code = source.code;

                active.description = source.description;

                active.date_deleted = source.date_deleted;

                active.update();

                updated = true;

            }

        }

 

        if (!updated) {

            var newRec = new GlideRecord('my_table_name');

            newRec.initialize();

            newRec.name = recordName;

            newRec.status = source.status;

            newRec.company = source.company;

            newRec.code = source.code;

            newRec.description = source.description;

            newRec.date_deleted = source.date_deleted;

            newRec.insert();

        }

    } catch (e) {

        log.error('Transform script failed: ' + e.message);

    }

})(source, map, log, target);