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

I missed to update this line "\

rec.ci_item = current.cmdb_ci;

But anyways, you got the resolution, so great!

Best Regards
Aman Kumar

Jaspal Singh
Mega Patron
Mega Patron

Did you create a new field to add mutliple CIs or changed the existing cmdb_ci field's field type to List from Reference?

Hi Jaspal,

I created a new reference field u_configuration_items_list (and replaced the fieldname cmdb_ci in the script above).

Can you share the script you updated once for a check.

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