Auto refresh Impacted Services

Anjani Anand1
Kilo Contributor

I want all the impacted Services of a change record updated automatically every time there is a change in 'Affected CIs' or 'Business Service' so that I can identified the impacted Areas and Notify correct product owners about the exposure to their Services.

If the risk of changes then the record has to set back to "New" for processing again.

2 REPLIES 2

Hardik Benani
Mega Sage
Mega Sage

You can use a after business rule on the table with condition when current ci changes. Example below.

function onAfter(current, previous) {
   //This function will be automatically called when this rule is processed.
   removeAffectedServices(current);
	if (!current.cmdb_ci.nil())
	   addAffectedServices(current);
}

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();
}

function addAffectedServices(current) {
   var ciu = new global.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();
   }
}

 

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Just an FYI - With this code, if there are multiple services being added under the same task and these are non-duplicated, I was seeing unique key violation errors. Got around the issue by introducing the following code in addAffectedServices function, 

 

        var allServices = ciu.cisAffectedByTask(current);
        var services = (new global.ArrayUtil()).unique(allServices);