
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:23 AM
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();
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:51 AM
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();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 08:20 AM
I missed to update this line "\
rec.ci_item = current.cmdb_ci;
But anyways, you got the resolution, so great!
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:35 AM
Did you create a new field to add mutliple CIs or changed the existing cmdb_ci field's field type to List from Reference?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:43 AM
Hi Jaspal,
I created a new reference field u_configuration_items_list (and replaced the fieldname cmdb_ci in the script above).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:47 AM
Can you share the script you updated once for a check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:51 AM
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();
}
}