How can I insert a record into cmdb_rel_ci using the sys_id for the reference columns?

Todd O
Tera Guru

I'm using the web service api to load VM ESX servers, VM instances, etc.   After my load, I'm also trying to build the relationships between the VM records by using the cmdb_rel_ci table.   This table holds three primary references: 1) the parent record (ci ref), 2) the child record (ci ref), and 3) the relationship type (ci rel ref).   I'm doing this with a script and I first verify that I can find all three records before I attempt to insert a record into the cmdb_rel_ci table. But I'm having trouble inserting the record. More detail below.

I've umpteen ways to do this insert and I have a simplified sample below.   I cannot use the SetDisplayValue for the reference columns because unfortunately, my ServiceNow instance will return multiple values

First example doesn't work because my SN instance will give me the wrong CI for the parent.name (I have more than one CI with the same name for display value).

  function setRelationshipValues(parent, child, relationshipType) {

  var rel_ci = new GlideRecord("cmdb_rel_ci");

  rel_ci.initialize();

  rel_ci.parent.setDisplayValue(parent.name);

  rel_ci.child.setDisplayValue(child.name);

  rel_ci.type.setDisplayValue(relationshipType.name);

  var sysId = rel_ci.insertWithReferences();

Second example is what I want but this doesn't work either and gives me an error. Since I can query and get the sys_id's that I want, why can I not set these reference fields to these values?

  function setRelationshipValues(parent, child, relationshipType) {

  gs.info("Relationship Insert attempt. parent(" + parent.sys_id + "), child(" + child.sys_id + "), relationship(" + relationshipType.sys_id + ")");

  var rel_ci = new GlideRecord("cmdb_rel_ci");

  rel_ci.initialize();

  rel_ci.parent.sys_id = parent.sys_id;

  rel_ci.child.sys_id = child.sys_id;

  rel_ci.type.sys_id = relationshipType.sys_id;

  var sysId = rel_ci.insertWithReferences()

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

You need to take out the sys_ids on the left. Also, just use insert().


  function setRelationshipValues(parent, child, relationshipType) {  


  gs.info("Relationship Insert attempt. parent(" + parent.sys_id + "), child(" + child.sys_id + "), relationship(" + relationshipType.sys_id + ")");    


  var rel_ci = new GlideRecord("cmdb_rel_ci");  


  rel_ci.initialize();  


  rel_ci.parent = parent.sys_id;    


  rel_ci.child = child.sys_id;    


  rel_ci.type = relationshipType.sys_id;  


  var sysId = rel_ci.insert();


View solution in original post

2 REPLIES 2

Geoffrey2
ServiceNow Employee
ServiceNow Employee

You need to take out the sys_ids on the left. Also, just use insert().


  function setRelationshipValues(parent, child, relationshipType) {  


  gs.info("Relationship Insert attempt. parent(" + parent.sys_id + "), child(" + child.sys_id + "), relationship(" + relationshipType.sys_id + ")");    


  var rel_ci = new GlideRecord("cmdb_rel_ci");  


  rel_ci.initialize();  


  rel_ci.parent = parent.sys_id;    


  rel_ci.child = child.sys_id;    


  rel_ci.type = relationshipType.sys_id;  


  var sysId = rel_ci.insert();


Thank you, thank you, thank you, thank you!  


That was the ticket and I feel like a numb skull.