CMDBTransformUtil in Transf. Map onBefore script incorrectly switching CI class from child to parent

AntonioG4352175
Tera Expert

We are using the CMDBTransformUtil() API within an onBefore transform map script to process records through the CMDB Identification and Reconciliation Engine (IRE). The transform map is targeted to the cmdb_ci_server table.


When running the transform, existing CIs that belong to a child class, Linux servers on table cmdb_ci_linux_server, are being incorrectly upgraded/changed to the parent class cmdb_ci_server instead of remaining on their original subclass. The reconciliation rule is configured for cmdb_ci_server, but the class should not be downgraded or upgraded automatically.

Existing CIs should retain their current class (cmdb_ci_linux_server), IRE should update attributes without switching the CI to a parent class.

Matched CIs are being moved from cmdb_ci_linux_server to cmdb_ci_server after cmdbUtil.identifyAndReconcile() executes.

Is there a property or setting within CMDBTransformUtil / IRE to prevent class switching?
What is the recommended approach to keep the existing CI class while still using the IRE via CMDBTransformUtil?

Script:

var cmdbUtil = new CMDBTransformUtil();
    cmdbUtil.setDataSource('ImportSet');
    cmdbUtil.identifyAndReconcileEnhanced(source, map, log);
    ignore = true;

    if (cmdbUtil.hasError()) {
        var errorMessage = cmdbUtil.getError();
        log.error(errorMessage);
    } else {
        log.info('IE Output Payload: ' + cmdbUtil.getOutputPayload());
        log.info('Imported CI: ' + cmdbUtil.getOutputRecordSysId());
    }
1 ACCEPTED SOLUTION

AntonioG4352175
Tera Expert

Here is the implementation of onBefore script:

    var cmdbUtil = new CMDBTransformUtil();
    cmdbUtil.setDataSource('importset');

    // Prevent the Transform Map from updating/inserting the target directly
    ignore = true;

    try {

        // Get the payload generated by the Transform Map
        var payload = cmdbUtil.getPayload(source, map, log);

        if (!payload) {
            log.error('Empty payload. Processing aborted.');
            return;
        }

        // getPayload() returns an object in this environment
        if (!payload.items || payload.items.length === 0) {
            log.error('No items found in payload. Processing aborted.');
            return;
        }

        var item = payload.items[0];
        var serialNumber = item.values.serial_number;

        if (!serialNumber) {
            log.error('Serial number is empty. Processing aborted.');
            return;
        }

        /*
         * Find the existing CI by serial number.
         * The purpose is to identify the CI's actual class before
         * sending the payload to the IRE.
         */
        var ciGR = new GlideRecord('cmdb_ci_hardware');
        ciGR.addQuery('serial_number', serialNumber);
        ciGR.query();

        if (!ciGR.next()) {
            log.error(
                'CI not found for serial number: ' +
                serialNumber +
                '. No insert will be performed.'
            );
            return;
        }

        var existingClass = ciGR.getValue('sys_class_name');
        var ciSysId = ciGR.getUniqueValue();

        /*
         * Preserve the existing CI class.
         *
         * The Transform Map target table is cmdb_ci_computer,
         * but the existing CI may belong to a more specific class,
         * such as cmdb_ci_linux_server.
         *
         * Without this adjustment, IRE may return UPDATE_WITH_DOWNGRADE.
         */
        item.className = existingClass;

        /*
         * Send the modified payload back to CMDBTransformUtil.
         */
        cmdbUtil.setPayload(payload);

        /*
         * Execute Identification and Reconciliation.
         */
        cmdbUtil.identifyAndReconcileEnhanced(source, map, log);

        /*
         * Log only relevant information.
         */
        if (cmdbUtil.hasError()) {

            log.error(
                'IRE error for CI ' +
                ciSysId +
                ': ' +
                cmdbUtil.getError()
            );

        } else {

            log.info(
                'CI updated successfully. ' +
                'Sys ID: ' + ciSysId +
                ', Class: ' + existingClass
            );
        }

    } catch (ex) {

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

    }

View solution in original post

3 REPLIES 3

abbasshaik4
Tera Sage

Hello @AntonioG4352175,

 

Please refer to the link below:
CMDBTransformUtil - Global • Brazil API Reference • Docs | ServiceNow

 

If it is helpful, please mark it as helpful and accept the correct solution by referring to this solution in the future it will be helpful to them.

 

Thanks & Regards,

Abbas Shaik

@abbasshaik4 

Thank you for your reply!

This is the document I am using as a reference to create the onBefore script, where I am facing the issue mentioned above.

AntonioG4352175
Tera Expert

Here is the implementation of onBefore script:

    var cmdbUtil = new CMDBTransformUtil();
    cmdbUtil.setDataSource('importset');

    // Prevent the Transform Map from updating/inserting the target directly
    ignore = true;

    try {

        // Get the payload generated by the Transform Map
        var payload = cmdbUtil.getPayload(source, map, log);

        if (!payload) {
            log.error('Empty payload. Processing aborted.');
            return;
        }

        // getPayload() returns an object in this environment
        if (!payload.items || payload.items.length === 0) {
            log.error('No items found in payload. Processing aborted.');
            return;
        }

        var item = payload.items[0];
        var serialNumber = item.values.serial_number;

        if (!serialNumber) {
            log.error('Serial number is empty. Processing aborted.');
            return;
        }

        /*
         * Find the existing CI by serial number.
         * The purpose is to identify the CI's actual class before
         * sending the payload to the IRE.
         */
        var ciGR = new GlideRecord('cmdb_ci_hardware');
        ciGR.addQuery('serial_number', serialNumber);
        ciGR.query();

        if (!ciGR.next()) {
            log.error(
                'CI not found for serial number: ' +
                serialNumber +
                '. No insert will be performed.'
            );
            return;
        }

        var existingClass = ciGR.getValue('sys_class_name');
        var ciSysId = ciGR.getUniqueValue();

        /*
         * Preserve the existing CI class.
         *
         * The Transform Map target table is cmdb_ci_computer,
         * but the existing CI may belong to a more specific class,
         * such as cmdb_ci_linux_server.
         *
         * Without this adjustment, IRE may return UPDATE_WITH_DOWNGRADE.
         */
        item.className = existingClass;

        /*
         * Send the modified payload back to CMDBTransformUtil.
         */
        cmdbUtil.setPayload(payload);

        /*
         * Execute Identification and Reconciliation.
         */
        cmdbUtil.identifyAndReconcileEnhanced(source, map, log);

        /*
         * Log only relevant information.
         */
        if (cmdbUtil.hasError()) {

            log.error(
                'IRE error for CI ' +
                ciSysId +
                ': ' +
                cmdbUtil.getError()
            );

        } else {

            log.info(
                'CI updated successfully. ' +
                'Sys ID: ' + ciSysId +
                ', Class: ' + existingClass
            );
        }

    } catch (ex) {

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

    }