CI Relationship-Query to get the root CI

Abilash Vangara
Mega Expert


Hi,

 

How can a record in CI Relationships(cmdb_rel_ci) table can be queried to get the root CI to which all these sub CI's are related to. When I mean sub CI that can be at any level, i.e it can be a direct child to the root CI or nested child CI at 3rd or 4th level. No matter at any level or stage the CI is related to the parent CI I need the list of all the parent CI's till the root CI.

 

For more clear explanation refer to the screen shot below

CI Realtionship.PNG

 

Please share your Ideas....!

1 ACCEPTED SOLUTION


Hello Mathan,



I made the below changes in one of the function definitions on the script include (CiUtils2) defined by Mark Stanger(above link)



getCIXML: function(id) {


            var gr = new GlideRecord('cmdb_rel_ci');


            gr.addQuery('child', id);


            gr.query();


            gr.next();


            var str = '';


            //str += '<CI>';


          // str += '<sys_id>'+gr.child.sys_id+'</sys_id>';


            str += gr.child.name+'--';


  //str += '<name>'+gr.child.name+'--'+'</name>';


            //str += '<relType> SELF<br></relType>';


            ret = this._recurs(id);


            if(ret){


                // str += '<children>';


                  str += ret;


                // str += '</children>';


            }


           


            //str += '</CI>';


            return str;


      },



Then created a business rule to call the above function



var ciu = new CIUtils2();


var myXML = ciu.getCIXML(current.cmdb_ci);


var a=myXML.split("--");


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


     


      {


              gs.addInfoMessage(a[i]);


      }



This BR displays all the related CI's of that in the cmdb_ci field...


View solution in original post

12 REPLIES 12

Hello, can I get help from you on this ?


I have a problem that dont know how to populate service on incident form if CI has changed - onchange client script would be great for me



We have field cmdb_ci and u_service_ci


When on form is CI which is connected upstreamly to another CI, I cannot get Service on form and dont know how to do it.


Can you help me please ?



Relation I have is like



Service 1--> CI 1 --> CI 2 --> CI 3


If CI 3 is on form, I need to populate Service 1 on u_service _ci field



thank you for your time


/Petr


Hi,



Define the relation between all the CMDB tables then write a on change client script and use the below script include to get the value.


Also modify the query in the script include as per your requirment.



var getBusinessServicePEs = Class.create();


getBusinessServicePEs.prototype = Object.extendsObject(AbstractAjaxProcessor, {


     


     


      getBusinessService: function() {


              var device = this.getParameter('sysparm_device_name');


              var arr = device.split(",");


              var service = '';


              var parentS = '';


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


                      gs.log(arr[i] + " : device 1");


                     


                      if(service == ''){


                             


                              service = this.getParent(arr[i],parentS);


                              gs.log("service is " + service);


                      }


                      else {


                             


                              service = service + ',' + this.getParent(arr[i],parentS);


                              gs.log("Final service is : " + service);


                      }


              }


             


              return service;


      },


      getParent: function(pe,parentS){


              var array=[];


              var rel = new GlideRecord('cmdb_rel_ci');


//ADD query as oper your requriment              


rel.addQuery('parent','IN',pe);


              rel.addQuery('u_ci_type','Resource');


              rel.query();


              while(rel.next()){


                      var t = new GlideRecord('cmdb_rel_ci');


                      t.addQuery('child',rel.child);


                      t.query();


                      while(t.next()){


                              if(t.parent.sys_class_name == 'YOUR CUSTOM TABLE NAME'){


                                      array.push(t.getValue('parent'));


                              }


                      }


              }


             


              return array.toString();


      }


});


//Client script



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading || newValue == '') {


              return;


      }


      var service = '';


      var newService = '';


      var device = g_form.getValue('u_affected_device_ci');


      service = g_form.getValue('u_affected_business_service_ci');


      var ga1 = new GlideAjax('getBusinessServicePEs');


      ga1.addParam('sysparm_name','getBusinessService');


      ga1.addParam('sysparm_device_name',device);


      ga1.getXMLWait();


      var answer=ga1.getAnswer();


      var totalService = service+','+answer;


      g_form.setValue('u_affected_business_service_ci',totalService);      


}