Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Post Sensor script keeps throwing error

nicks8850
Tera Expert

Below is the script I have written to build the relationship between my generic application and SAP Central SErvices application. But it always ends up with post processing errors as below.

 

Post-sensor script "SAPTEST - Build Relationship" failed due to: TypeError: Cannot convert null to an object.

 

Here is the script:

/*
 * Post sensor: You can update/add missing info to the DB based on result (Json) from
 * Identification Engine
 */

//var rtrn = {}; 

 /* 
 * When the “use_split_payload” property is set to true, post-sensor script will receive a variable named “payloadRecords”
 *. This is a GlideRecord query where each next() call will return a piece of the payload from the HorizonatalDiscoverySensor. 
 */
/*
 * Post sensor
 */
/*
 * Post sensor: Create relationship between SAP Message/Enqueue Server
 * and SAP ASCS/SCS Application
 */

var rtrn = {};

while (payloadRecords.next()) {

    var ieOutput = JSON.parse(payloadRecords.getValue('ie_output'));

    var genericSysId = '';
    var ascsSysId = '';
    var relationSysId = '';
    var parentTable = '';

    if (!ieOutput.hasOwnProperty('items'))
        continue;

    var items = ieOutput.items;

    for (var i = 0; i < items.length; i++) {

        if (items[i].className == 'cmdb_ci_appl_generic') {
            genericSysId = items[i].sysId;
            break;
        }
    }

    if (!genericSysId) {
        prePostNodeLogger.info(prePostLogPrefix + ' Generic Application not found in payload');
        continue;
    }

    var genericGR = new GlideRecord('cmdb_ci_appl_generic');
    genericGR.addQuery('sys_id', genericSysId);
    genericGR.query();

    var sid = '';
    var ciName = '';
    var hostName = '';
    var instanceName = '';

    if (genericGR.next()) {

        sid = genericGR.getValue('serial_number');
        ciName = genericGR.getValue('name');

        if (ciName && ciName.indexOf('@') > -1) {
            hostName = ciName.split('@')[1];
        }
        var match = ciName.match(/^SAP (?:Message|Enqueue) Server\s+(.+?)\s+on\s+/);
		instanceName = match ? match : '';

    } else {
        prePostNodeLogger.info(prePostLogPrefix + ' Generic Application CI not found');
        continue;
    }

    if (!sid && !hostName && !instanceName) {
        prePostNodeLogger.info(prePostLogPrefix + ' Missing required values. SID=[' + sid +'] Host=[' + hostName +'] Instance=[' + instanceName + ']');
        continue;
    }

    if (instanceName.indexOf('ASCS') > -1) {
        parentTable = 'cmdb_ci_appl_sap_ascs';
    } else {
        parentTable = 'cmdb_ci_app_sap_ascs';
    }

    var ascsGR = new GlideRecord(parentTable);
    ascsGR.addQuery('sid', sid);
    ascsGR.addQuery('instance_name', instanceName);
    //ascsGR.addQuery('name', 'CONTAINS', hostName);
    ascsGR.query();

    if (ascsGR.next()) {
        ascsSysId = ascsGR.getUniqueValue();
    } else {
        prePostNodeLogger.info(prePostLogPrefix +' Matching ASCS/SCS CI not found. SID=[' + sid +'] Host=[' + hostName +'] Instance=[' + instanceName + ']');
        continue;
    }

    var relTypeGR = new GlideRecord('cmdb_rel_type');
    relTypeGR.addQuery('name', 'Contains::Contained by');
    relTypeGR.query();

    if (relTypeGR.next()) {
        relationSysId = relTypeGR.getUniqueValue();
    } else {
        prePostNodeLogger.info(prePostLogPrefix + ' Relationship Type not found');
        continue;
    }

    var relGR = new GlideRecord('cmdb_rel_ci');
    relGR.addQuery('parent', ascsSysId);
    relGR.addQuery('child', genericSysId);
    relGR.addQuery('type', relationSysId);
    relGR.query();

    if (!relGR.next()) {

        relGR.initialize();
        relGR.setValue('parent', ascsSysId);
        relGR.setValue('child', genericSysId);
        relGR.setValue('type', relationSysId);
        relGR.insert();

        prePostNodeLogger.info(prePostLogPrefix +' Relationship created. Parent=[' +ascsSysId +'] Child=[' +genericSysId +']');
    }
}

// You can return a message and a status, on top of the input variables that you MUST return.
// Returning the payload as a Json String is mandatory in case of a pre sensor script, and optional in case of post sensor script.
// If you want to terminate the payload processing due to your business logic - you can set isSuccess to false.
rtrn = {
	'status': {
		'message': 'SAP relationship processing completed',
		'isSuccess' :true
	},
	'patternId': patternId
};

 

0 REPLIES 0