
- 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 06:29 AM
Hey
So what happens here is when you are adding in the list, the addCurrentCI would run only once, since it doesn't know if its a multiple CIs you are planning to add.
What you need to do here is to have a for loop in place, which will fetch the value of your list and then one by one you it will iterate through your list of CIs and create your Affected CIs records.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:45 AM
Hi Aman,
I supposed so; can you point me in the right direction/documentation/example how to do it? I have limited programming experience.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 06:51 AM
For sure, your structure would remain the same;
all you want to do is, is update addCurrentCI.
function addCurrentCI() {
//Create Affected CI record for this task and current CI
var listCIs = current.getValue("your_ci_list_field_name");// update the name of field
var arrofCI = listCIs.split(",");
for(var x in arrofCI){
var rec = new GlideRecord('task_ci');
rec.addQuery('task', current.sys_id);
rec.addQuery('ci_item', arrofCI[x]);
rec.query();
if (rec.next())
return;
rec.initialize();
rec.task = current.sys_id;
rec.ci_item = current.cmdb_ci;
rec.insert();
}
}
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2022 07:09 AM