Add the Impacted Services from all Affected CI's to a Change

scott_mcdermott
Kilo Explorer

I have a related list on Task called 'Impacted Services' that lists the CI's related business services. It's populated by the following Business Rule:

removeAffectedServices();

if (!current.cmdb_ci.nil())

    addAffectedServices();

//Removes any affected Services currently on the list

function removeAffectedServices() {

    var m2m = new GlideRecord('task_cmdb_ci_service');

    m2m.addQuery('task',current.sys_id);

    m2m.addQuery('manually_added','false');

    m2m.query();

    m2m.deleteMultiple();

}

//Adds the affected Services based on the CI

function addAffectedServices() {

    var ciu = new CIUtils();

    var services = ciu.servicesAffectedByCI(current.cmdb_ci);

    var m2m = new GlideRecord('task_cmdb_ci_service');

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

          m2m.initialize();

          m2m.task = current.sys_id;

          m2m.cmdb_ci_service = services[i];

          m2m.manually_added = 'false';

          m2m.insert();

    }

}

But... it only brings back the Business Service from the main CI field - 'cmdb_ci', and nothing from the Affected CIs table 'task_ci'. I need it to bring back all services from all the affected CI's. For example, if additional CI's get added to a Change record, then the Impacted Services list should also upate to reflect the potential new affected services.

Has anyone acheived this or something similar and able to offer an insight on what I need to change.

Thanks

12 REPLIES 12

Kalaiarasan Pus
Giga Sage

This will give you affected CI's...



var affectedCI = [];


var affectedItem = new GlideRecord('task_ci');


affectedItem.addQuery('task',current.sys_id);


affectedItem.query();



while (affectedItem.next()) {


affectedCI.push(affectedItem.ci_item.toString());


}


Thanks Kalairasan,



Unfortunately that doens't give me any of the impacted services.



Perhaps the screenshots below will help explain better, in the following example I have 2 Affected CI's on my Change record and they are related to 2 seperate Business Services... however only the Business Service from the primary CI appears on the Impacted Service list.



3.PNG




4.PNG



To be truely useful, I need the Business Rule to return all related Business Services from all the affected CIs.



Many thanks


The script would give you the id's of the affected CI... You need to use the function that is already present in your script to get the services.


var ciu = new CIUtils();


var services = ciu.servicesAffectedByCI('Pass the individual id's of the affected CI's');


epam
Kilo Guru

I combined and checked the code above. The following script works fine for the onDisplay Business Rule on the Incident table:



function onDisplay(current, g_scratchpad) {


      removeAffectedServices(current);


      if (!current.cmdb_ci.nil()) {


      addAffectedServices(current);


      }


}



//Removes any affected Services currently on the list


function removeAffectedServices(current) {



    var m2m = new GlideRecord('task_cmdb_ci_service');


    m2m.addQuery('task',current.sys_id);


    m2m.addQuery('manually_added','false');


    m2m.query();


    m2m.deleteMultiple();



}



//Adds the affected Services based on the CI


function addAffectedServices(current) {



    var affectedItem = new GlideRecord('task_ci');


    affectedItem.addQuery('task', current.sys_id);


    affectedItem.query();


    var aff_ci;



    while (affectedItem.next()) {



          aff_ci = affectedItem.ci_item.toString();


          var ciu = new CIUtils();


          var services = ciu.servicesAffectedByCI(aff_ci);    


          var m2m = new GlideRecord('task_cmdb_ci_service');


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


                    m2m.initialize();


                    m2m.task = current.sys_id;


                    m2m.cmdb_ci_service = services[i];


                    m2m.manually_added = 'false';


                    m2m.insert();        


          }



    }


}