cmdb relations

Naveen87
Tera Guru

Hi Developers,

 

I have a requirement, Where I need to add multiple services as child from (cmdb_ci_service_technical) table to Business Application(cmdb_ci_business_app) table.

It can be done manually but I will have 500-700 records.

 

Please suggest best & less complicated way to achieve this.

Naveen87_0-1683905917761.png

 

 

Thank you.

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Naveen87 

Why don't you create a import set and prepare an ecel and import at once in the Relationship CI table.

I think that will be the best possible way to achieve this. No scripting, no hassle.

 

Best Regards
Aman Kumar

View solution in original post

4 REPLIES 4

Joe S1
Kilo Sage

Hello,

 

You could run a background or fix script if all of your CIs are already created in the cmdb. Take the sys_id of the business application and loop through all the CIs that need to be a child and create a record in the cmdb_rel_ci table where parent = business app and child = ci record and type is whatever relationship type you're defining.

 

Thanks,

 

Joe S

Hi Joe,

 

Thank you for the response. 

I'm not good at scripting.

Please help me with the script.

 

 

I tried this but I don't think it's correct.

 

var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('number','sys_id');
gr.query();
while gr.next(){
gr.initialize();
gr.child = 'xyz' //ci name or sys_id
gr.insert();
}
 
 
 
Thank you.

Hi Naveen,

 

Aman is correct you could do an import set and create the records in the relationship table that way. Since you're CI records already exist that would require exporting them out to a file and then importing into the rel_ci table. 

 

If you want to go the script route, here is an example of how you could script this in a fix script.

 

var busApp = 'cc45f84e4732211004ed5882e36d4392'; //sys_id of your business application record

//you need to get all of your CI records that need to have a relationship to the business application
//if it's not ALL of them then an encoded query is probably easiest.
var encQry = 'os=Windows 2003 Standard'; //example query string

//query the cmdb_ci_service_technical table for all of your child records.
var ciRecs = new GlideRecord('cmdb_ci_win_server'); //You'll be querying the "cmdb_ci_service_technical" table here.
ciRecs.addEncodedQuery(encQry);
ciRecs.query();

while (ciRecs.next()) {
    //now that we have the children, while loop through them to create the relationship.
    var gr = new GlideRecord('cmdb_rel_ci');
    gr.initialize();
    gr.parent = busApp; //set the parent sys_id to the business application
    gr.child = ciRecs.sys_id; //set the child to the sys_id of the CI record
    gr.type = '25242fb2377a9200738d021a54990e88'; //sys_id of the "type" of relationship example here is Owns::Owned by
    gr.insert();
}

gs.info("Number of relationships created - " + ciRecs.getRowCount());

 

Aman Kumar S
Kilo Patron

Hi @Naveen87 

Why don't you create a import set and prepare an ecel and import at once in the Relationship CI table.

I think that will be the best possible way to achieve this. No scripting, no hassle.

 

Best Regards
Aman Kumar