How can I have this business rule execute for each item that has been added to the list or removed from the list?

Jonathan Demeul
Kilo Guru

Hi,

We have a requirement to add multiple CI's to an incident. As is often the case sometimes we have an incident affected for example 5 computers so we want to be able to add 5 CIs to the incident instead of having to add 1 on the form and the rest in the affected CI.

Therefore I have created a reference list on the incident form which simply allows my agents to add multiple CIs in one go. I wanted to utilize the existing business rule that was configured for the OOB one-to-one field for CIs: 
(https://yourinstance.service-now.com/nav_to.do?uri=sys_script.do?sys_id=6f8daf204a3623120146c79f308310df)

However this only works if I add the CIs one at a time and save the form each time, how can I change the script so it executes for each item in the list? This is the current code:

if (!previous.cmdb_ci.nil())
   removePreviousCI();

if (!current.cmdb_ci.nil())
   addCurrentCI();

function removePreviousCI() {
   // Delete Affected CI records for this task and previous CI
   var rec = new GlideRecord('task_ci');
   rec.addQuery('task', current.sys_id);
   rec.addQuery('ci_item', previous.cmdb_ci);
   rec.query();
   while (rec.next())
      rec.deleteRecord();
}

function addCurrentCI() {
   //Create Affected CI record for this task and current CI
   var rec = new GlideRecord('task_ci');
   rec.addQuery('task', current.sys_id);
   rec.addQuery('ci_item', current.cmdb_ci);
   rec.query();
   if (rec.next())
      return;

   rec.initialize();
   rec.task = current.sys_id;
   rec.ci_item = current.cmdb_ci;
   rec.insert();
}
1 ACCEPTED SOLUTION

Assuming you have field replaced in last function. Replace last function with below

function addCurrentCI() {
   //Create Affected CI record for this task and current CI

var cmdbciis=current.u_configuration_items_list.toString().split(',');

for(var i=0;i<cmdbciis.length;i++)
{
 var rec = new GlideRecord('task_ci');
   rec.addQuery('task', current.sys_id);
   rec.addQuery('ci_item', cmdbciis[i]);
   rec.query();
   if (rec.next())
      return;

   rec.initialize();
   rec.task = current.sys_id;
   rec.ci_item = cmdbciis[i];
   rec.insert();
}
}

View solution in original post

12 REPLIES 12

Hi,

Your solution worked! Thank you very much

However, when I now delete a CI from that list, it does not delete it in the affected CI list..

I tried to apply the same logic as above but it does not seem to work.

I suggest yo uopen a separate thread for deletion request if needed. As this would avoid confusion for future readers as well if they look for thread & corresponding solution.