yjaques
Tera Contributor

Sometimes you may automatically want to generate a closely related CI to an existing CI and you don't feel like it doing it all step by step in the interface, maybe you have too many or maybe you want it to run as a business rule. Recently we had a number of Java applications set as CIs for which we wanted to create a parallel set of business services and relate the business service CI to the Java app CI. There were about 35 of them so I thought I would script it instead of going through all those forms. Here's what I did:

//create an object in my Java CI table

var app = new GlideRecord('u_cmdb_ci_applications_web_jav');

//iterate through every record

app.query();

while (app.next()) {

      //make sure record meets your criteria (in my case had to be operational and installed

        if(app.operational_status==1 && app.install_status==1) {

                //create a service object

                  var serv = new GlideRecord('cmdb_ci_service');

                  serv.initialize();

                  //give it the same name as the app

                  serv.name = app.name;

                  //create the CI record

                  servId = serv.insert();

                  if(servId != null) {

                            gs.log('Added SERVICE!');

                            //create a relationship object

                            var rel = new GlideRecord('cmdb_rel_ci');

                            rel.initialize();

                            //make the parent the service

                            rel.parent = servId;

                            //make the child the app

                            rel.child = app.sys_id;

                            //define the relationship type (in my case was Depends on:: Used by)

                            rel.type = 1;

                          //create the relationship

                            relId = rel.insert();

                            if(relId != null) {

                                      gs.log('Added rel!');

                            }

                  }

        }

}