Script for creating relationship between server and IP Address.

Prem Ravindrana
Mega Contributor

Hello Experts,

Could you please assist me building the logic for following.  

1. Saving the hostname and IP Address

2. Relationship Type Server owns::owned by IPAddress

3. Using the variables we need to find the sys_ids for these records.

4. check if the relationship types already exists in the cmdb_rel_ci table.

5. If (relationship exists) - do nothing

6. If (relationship doesn't exists) - create relationship.

 

(function transformRow(source, target, map, log, isUpdate) {

// Add your code here

function findCISysID(){
grCI = new GlideRecord('');
grCI.query('Name');
while (grCI.next()){
gs.print('CI Name: '+grCI.name+ ', Sys ID: ' +grCI.sys_id);
}
}


var parentCI = source.hostname
var childCI = source.IPAddress
var relationshipType = Owns::Owned by;

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

for(var j=0; j<childCI.length; j++){
//Check if relationship already exists
var relRec = new GlideRecord('cmdb_rel_ci');
relRec.addQuery('type.name', relationshipType);
relRec.addQuery('parent', hostname[i]);
relRec.addQuery('child', IPAddress[j]);
relRec.query();

if(relRec.next()){
//Relationship already present Do not create new relation
}
else{
//Create Relationship
var relRec1 = new GlideRecord('cmdb_rel_ci');
relRec1.initialize();
relRec1.parent = hostname[i];
relRec1.child = IPAddress[j];
relRec.type = findCISysID();
relRec.insert();
}
}
}
})(source, target, map, log, action==="update");

 

I used the above logic but it did not work, please help me with the logic or the idea?

 

Best Regards,

1 ACCEPTED SOLUTION

Prem Ravindrana
Mega Contributor

Hi Forum Members, this is a tested working script for the issue.  

 


function convertNameToId(table, fieldname) {
var sysId = '';
var tableGr = new GlideRecord(table);
tableGr.addQuery('name', fieldname);
tableGr.query();
if (tableGr.next()){
gs.debug("Inside my if statement");
sysId = tableGr.sys_id;
gs.debug("tableGr.sys_id" +tableGr.sys_id);
}
gs.debug("sysId" +sysId);
return sysId;
}

function checkRelExists(parent_sysId, child_sysId, reltype_sysId) {

var relRec = new GlideRecord('cmdb_rel_ci');
relRec.addQuery('parent',parent_sysId);
relRec.addQuery('child',child_sysId);
relRec.addQuery('type',reltype_sysId);
relRec.query();
if (relRec.next()) {
gs.debug("inside my second if else");
return false;
}
else {
return true;

}
}
// 1

gs.debug("Beginning of the script");

var parent = source.u_hostname.toString();
var child = source.u_ipaddress.toString();
var reltype = "Owns::Owned by";
gs.debug("parent");
gs.debug("child");
gs.debug("reltype");

// 2

var parentSysId = convertNameToId('cmdb_ci_server', parent);
var childSysId = convertNameToId('cmdb_ci_ip_address', child);
var reltypeSysId = convertNameToId('cmdb_rel_type', reltype);
gs.debug("parentSysId" +parentSysId);
gs.debug("childSysId" +childSysId);
gs.debug("reltypeSysId" +reltypeSysId);

//3

var existsBool = checkRelExists(parentSysId, childSysId, reltypeSysId);

if(existsBool) {
gs.debug("inside my third if else");
target.parent=parentSysId;
target.child=childSysId;
target.type=reltypeSysId;
gs.debug("parentSysId" + parentSysId);
gs.debug("childSysId" + childSysId);
gs.debug("reltypeSysId" + reltypeSysId);
} else {

var relRec1 = new GlideRecord('cmdb_rel_ci');
relRec1.initialize();
relRec1.parent = parentSysId;
relRec1.child = childSysId;
relRec1.type = reltypeSysId;
gs.debug("relRec1.parent" +relRec1.parent);
gs.debug("relRec1.child" +relRec1.child);
gs.debug("relRec1.type" +relRec1.type);
relRec1.insert();
}

gs.debug("End of the script");

View solution in original post

4 REPLIES 4

vkachineni
Kilo Sage
Kilo Sage

//Untested code. Made some modifications

 

(function transformRow(source, target, map, log, isUpdate) {

    // Add your code here
    var parentCI = source.hostname; // Assuming we are getting name like 'ThinkStation S20'
    var childCI = source.IPAddress; //	Asuming we get ip address 127.0.0.1
    var relationshipType = "Owns::Owned by"; //update with the sys_id of Owns::Owned by; from  task_rel_type table	
	var relationshipTypeSys_id = findCISysID(relationshipType);
	
	//if there is a parent and child & rel sys_id
	if(parentCI.toString() != '' && childCI.toString() != '' && relationshipTypeSys_id.toString() != '')
	{
			//Check if relationship already exists
            var relRec = new GlideRecord('cmdb_rel_ci');
			
			//If going by type name Owns::Owned by
			//"parent.name=" + parentCI + "^child.name=" + childCI + "^type.name=" + relationshipType;			
			relRec.addEncodedQuery("parent.name=" + parentCI + "^child.name=" + childCI + "^type.name=" + relationshipTypeSys_id);		
			relRec.query();
			
			// No relation is found. Create one.
			if (!relRec.next()) {
				
				//Create Relationship
                var relRec1 = new GlideRecord('cmdb_rel_ci');
                relRec1.initialize();
                relRec1.parent = parentCI;
                relRec1.child = childCI;
                relRec.type = relationshipTypeSys_id;
                relRec.insert();                
            } 		
	}
	
	function findCISysID(relationshipType) {
        grCI = new GlideRecord('task_rel_type');
		relRec.addEncodedQuery("name=" + relationshipType);			
        grCI.query();
        if (grCI.next()) 
		{
            gs.print('CI Name: ' + grCI.name + ', Sys ID: ' + grCI.sys_id);
			return grCI.sys_id;
        }
		
    }
	
	
})(source, target, map, log, action === "update");

 

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

thanks @vkachineni, i tried the code.  It did not work, have you tried it at your end.  is it working?

 

Prem Ravindrana
Mega Contributor

Hi Forum Members, this is a tested working script for the issue.  

 


function convertNameToId(table, fieldname) {
var sysId = '';
var tableGr = new GlideRecord(table);
tableGr.addQuery('name', fieldname);
tableGr.query();
if (tableGr.next()){
gs.debug("Inside my if statement");
sysId = tableGr.sys_id;
gs.debug("tableGr.sys_id" +tableGr.sys_id);
}
gs.debug("sysId" +sysId);
return sysId;
}

function checkRelExists(parent_sysId, child_sysId, reltype_sysId) {

var relRec = new GlideRecord('cmdb_rel_ci');
relRec.addQuery('parent',parent_sysId);
relRec.addQuery('child',child_sysId);
relRec.addQuery('type',reltype_sysId);
relRec.query();
if (relRec.next()) {
gs.debug("inside my second if else");
return false;
}
else {
return true;

}
}
// 1

gs.debug("Beginning of the script");

var parent = source.u_hostname.toString();
var child = source.u_ipaddress.toString();
var reltype = "Owns::Owned by";
gs.debug("parent");
gs.debug("child");
gs.debug("reltype");

// 2

var parentSysId = convertNameToId('cmdb_ci_server', parent);
var childSysId = convertNameToId('cmdb_ci_ip_address', child);
var reltypeSysId = convertNameToId('cmdb_rel_type', reltype);
gs.debug("parentSysId" +parentSysId);
gs.debug("childSysId" +childSysId);
gs.debug("reltypeSysId" +reltypeSysId);

//3

var existsBool = checkRelExists(parentSysId, childSysId, reltypeSysId);

if(existsBool) {
gs.debug("inside my third if else");
target.parent=parentSysId;
target.child=childSysId;
target.type=reltypeSysId;
gs.debug("parentSysId" + parentSysId);
gs.debug("childSysId" + childSysId);
gs.debug("reltypeSysId" + reltypeSysId);
} else {

var relRec1 = new GlideRecord('cmdb_rel_ci');
relRec1.initialize();
relRec1.parent = parentSysId;
relRec1.child = childSysId;
relRec1.type = reltypeSysId;
gs.debug("relRec1.parent" +relRec1.parent);
gs.debug("relRec1.child" +relRec1.child);
gs.debug("relRec1.type" +relRec1.type);
relRec1.insert();
}

gs.debug("End of the script");

masood1
Tera Contributor

I tried both scripts

1- Tested script shared by Prem Ravindranath

2- Untested script shared by vkachineni

In both cases I am getting error "Operation against file 'cmdb_rel_ci' was aborted by Business Rule 'Prevent Recursion in CI Relationships^91eb95ab87bd119850d2eca73cbb352a'. Business Rule Stack:Prevent Recursion in CI Relationships"