The CreatorCon Call for Content is officially open! Get started here.

OnBefore Script import_set undefined

sumanth maram
Tera Contributor

Hey Everyone, I am working on a VI integration and am having issues with setting up the OnBefore execution script in the transformation mapping. Here is a copy of the script and the error. Any help would be appreciated! 

 

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

    // Handle re-opened VIs
    if (action == "update" && target.state == 3) {
        // We are re-opening
        var now = new GlideDateTime();
        target.state = '1';
        target.substate = '';
        target.reopened = true;
        target.last_opened = now.getDisplayValue();
    }

    //populate ci
    var integrationRun = import_set.integrationProcessGr.integration_run;
    var sourceInstance = integrationRun.implementation;
    var integration = integrationRun.implementation.integration;
    var hostAttributeTable =  integration.host_attributes_table;

    target.integration = integration;
    target.integration_instance = sourceInstance;
    target.integration_run = integrationRun;

    var host = {};

    for (var key in source)
        if (key.startsWith("u_"))
            host[key.substring(2).toUpperCase()] = source.getValue(key);

    host["MAC_ADDRESS"] = source.getValue("u_mac_address");
    host["HOST_NAME"] = source.getValue("u_host_name");
    host["ID"] = source.getValue("u_dev_id");

    
    var hostInfoGR = new sn_vul.HostUtil().initializeHostAttributeRecord(host, hostAttributeTable);

    var matchResult = new sn_vul.ImportHost().hostImport(
        integrationRun.implementation, host, "ID", integrationRun,
        false, false, source.getValue('u_last_assessed'), null, null, hostInfoGR);

	
    if (matchResult.insert)
        source.ci_operation = "inserted";
    else
        source.ci_operation = "updated";
    target.cmdb_ci = matchResult.sys_id;
    target.src_ci = matchResult.disc_item_id;

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

 

 

 

It errors out on this line  var integrationRun = import_set.integrationProcessGr.integration_run;

Here is the error: 


 

com.glide.script.RhinoEcmaError: Cannot read property "integration_run" from undefined
sys_transform_script.f717d64287fe39501aad4047cebb35e8.script : Line(14) column(0)
11: }
12:
13: //populate ci
==> 14: var integrationRun = import_set.integrationProcessGr.integration_run;
15: var sourceInstance = integrationRun.implementation;
16: var integration = integrationRun.implementation.integration;
17: var hostAttributeTable = integration.host_attributes_table;

 

8 REPLIES 8

Amit Gujarathi
Giga Sage
Giga Sage

HI @sumanth maram ,
I trust you are doing great.
The primary adjustment will be to add error handling to prevent accessing properties of an undefined object, which seems to be the cause of your error.

(function runTransformScript(source, map, log, target /*undefined onStart*/) {
    // Handle re-opened VIs
    if (action == "update" && target.state == 3) {
        // We are re-opening
        var now = new GlideDateTime();
        target.state = '1';
        target.substate = '';
        target.reopened = true;
        target.last_opened = now.getDisplayValue();
    }

    // Check if integrationProcessGr exists in import_set
    if (import_set && import_set.integrationProcessGr) {
        var integrationRun = import_set.integrationProcessGr.integration_run;
        var sourceInstance = integrationRun.implementation;
        var integration = integrationRun.implementation.integration;
        var hostAttributeTable = integration.host_attributes_table;

        target.integration = integration;
        target.integration_instance = sourceInstance;
        target.integration_run = integrationRun;

        var host = {};

        for (var key in source)
            if (key.startsWith("u_"))
                host[key.substring(2).toUpperCase()] = source.getValue(key);

        host["MAC_ADDRESS"] = source.getValue("u_mac_address");
        host["HOST_NAME"] = source.getValue("u_host_name");
        host["ID"] = source.getValue("u_dev_id");

        var hostInfoGR = new sn_vul.HostUtil().initializeHostAttributeRecord(host, hostAttributeTable);

        var matchResult = new sn_vul.ImportHost().hostImport(
            integrationRun.implementation, host, "ID", integrationRun,
            false, false, source.getValue('u_last_assessed'), null, null, hostInfoGR);

        if (matchResult.insert)
            source.ci_operation = "inserted";
        else
            source.ci_operation = "updated";
        
        target.cmdb_ci = matchResult.sys_id;
        target.src_ci = matchResult.disc_item_id;
    } else {
        log.error("integrationProcessGr is undefined in import_set");
        // Additional error handling or default actions can be added here
    }
})(source, map, log, target);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi Amit Gujarathi, 

Thank you for the reply. This is very useful for error handling but it does not solve the issue of import_set being undefined. I need to get the integrationRun inside of the OnBefore script. 

Tai Vu
Kilo Patron
Kilo Patron

Hi @sumanth maram 

That means the integration_run attribute doesn't exist or it returns undefined value.

Could you provide more information on what you are trying to retrieve with the following line?

 

var integrationRun = import_set.integrationProcessGr.integration_run;

 

 

Cheers,

Tai Vu 

Hi @Tai Vu , 

 

I am trying to get the integration run of the import set so that we can match/create new cis for vulnerable items.