Build relationship with transform map script

_bhishek
Tera Guru

 

 

 

 

 

 

Hello Experts,

 

I am receiving Serial number for EC2 CI ,Region  of AWS data center ,Account ID of Cloud service account through JDBC data source .We can find the EC2 CI with Serial Number from EC2CI table, AWS Datacenter CI with Region from AWS data center table, and find the Cloud Service Account CI with Account ID from cloud service account table We need to insert relationship as below in cmdb_rel_ci table.

  • EC2 Virtual Machine Instance (cmdb_ci_ec2_instance) Hosted on::Hosts AWS Datacenter (cmdb_ci_aws_datacenter) 
  • AWS Datacenter (cmdb_ci_aws_datacenter)  "Hosted on::HostsCloud Service Account CI (cmdb_ci_cloud_service_account). 

i.e.one inserted row from data source in import set will insert 2 records in cmdb_rel_ci table.

if any of required data is not received/empty  from the import i.e. if serial number/account Id or region is empty then whole row should be ignored and no relationship should be inserted for that row.There should be no duplicate relationship inserted .

I have written on before transform map script .But this is not working .Could you please help me here.

Thanks in advance

 

 

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

    gs.log("Inside function EC2");
    var sysIdec2;
    var sysIdaws;
    var sysIdcsa;
    //fetch source attributes from importset
    var sourceaccountid = source.u_accountid.toString();
    var sourceserialno = source.u_serialno.toString();
    var sourceregion = source.u_region.toString();
   // var typerel = '5f985e0ec0a8010e00a9714f2a172815';

    if (sourceaccountid == '' || sourceserialno == '' || sourceregion == '') {
        ignore = true;
    } else {
        gs.log("the values inside function " + sourceaccountid + sourceserialno + sourceregion);
        //glide query to fetch the sys id of parent and Child Ci's sysIds
        var parentec2 = new GlideRecord('cmdb_ci_ec2_instance');
        parentec2.addQuery('serial_number', sourceserialno);
        parentec2.query();
        if (parentec2.next()) {
            sysIdec2 = parentec2.sys_id; //Sys ID of serial number of ec2
        }

        var childaws = new GlideRecord('cmdb_ci_aws_datacenter');
        childaws.addQuery('region',sourceregion);
        childaws.query();
        if (childaws.next()) {
            sysIdaws = childaws.sys_id; //sysID from region of AWS data center
        }

        var childcsa = new GlideRecord('cmdb_ci_cloud_service_account');
        childcsa.addQuery('account_id', sourceaccountid);
        childcsa.query();
        if (childcsa.next()) {
            sysIdcsa = childaws.sys_id; //sysID from accountID of cloud service account
        }

        var cirel = new GlideRecord('cmdb_rel_ci');
        cirel.addQuery('parent',sysIdec2);
        cirel.addQuery('child',sysIdaws);
        cirel.addQuery('type','5f985e0ec0a8010e00a9714f2a172815');
        cirel.query();
        gs.log("details are " + sysIdec2, " " + sysIdaws, " " + typerel);
        if (cirel.next()) {
            ignore = true; //if the CI relationship already exist ignore the record
            gs.log("inside 1st if loop");
        } else {
            cirel.initialize();
            cirel.parent = sysIdec2;
            cirel.child = sysIdaws;
            cirel.type = '5f985e0ec0a8010e00a9714f2a172815';
            cirel.insert();

            gs.log("Inside Else loop");

        }

        var cirel2 = new GlideRecord('cmdb_rel_ci');
        cirel2.addQuery('parent', sysIdaws);
        cirel2.addQuery('child', sysIdcsa);
        cirel2.addQuery('type', '5f985e0ec0a8010e00a9714f2a172815');
        cirel2.query();
        gs.log("details are " + sysIdaws, " " + sysIdcsa, " " + typerel);
        if (cirel2.next()) {
            ignore = true; //if the CI relationship already exist ignore the record
            gs.log("inside 1st if loop");
        } else {

            cirel2.initialize();
            cirel2.parent = sysIdaws;
            cirel2.child = sysIdcsa;
            cirel2.type = '5f985e0ec0a8010e00a9714f2a172815';
            cirel2.insert();
        }
    }
})(source, map, log, target);

 

 

 

1 REPLY 1

Hayo Lubbers
Kilo Sage

Hi @_bhishek ,

 

You can try with the DiscoveryFunction script include, there is a "createRelationshipIfNotExists" function that does exactly what you need (and verifies existence).

 

Documentation: 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_DiscoveryFuncti...

 

Important, the relationshiptype is based on strings, not sysIds.

So, something like:

new global.DiscoveryFunctions().createRelationshipIfNotExists(sysIdec2, sysIdaws, "Hosted on:Hosts",true);

 

The last true seems to be undocumented, but it checks for uniqueness:

//checking unique for the newly created relationship
            if (ensureUnique)
                this.deleteRelationshipIfMultipleExists(parent, child, descriptor);

 

Hope this helps,

Hayo