List Collector to Populate Affected CI's Related List

stvillil
Kilo Contributor

Hi Everyone!

I have a requirement to add in a glide list field on the change form (the list collector with a lock in it) to populate the affected CIs related list.

I copied modified two business rules that is populating the related list, but it is adding the first choice over and over again and not adding the other CIs. I'm thinking I'd have to copy and modify the script include "ChangeCollisionHelper". Any ideas?


Sync CI in affected Cis List Picker



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

if (!current.u_additional_affected_cis.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.u_additional_affected_cis);
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.u_additional_affected_cis);
rec.query();
if (rec.next())
return;

rec.initialize();
rec.task = current.sys_id;
rec.ci_item = current.u_additional_affected_cis;
rec.insert();
}



Add CI in affected Cis List Picker



perform();

function perform(){
if(!ChangeCollisionHelper.isCiInAffectedCis(current.u_additional_affected_cis,current.sys_id)){
ChangeCollisionHelper.addCiToChangeAffectedCis(current.u_additional_affected_cis,current.sys_id);
}
}




Thanks in advance for any help you can provide.

9 REPLIES 9

Hi,



Did you ever find a solution to this as I have a similar requirement?



Thanks


Dan


Chuck Tomasi
Tera Patron

Anywhere you see something like:



current.sys_id



Change it to



current.getValue('sys_id')



Think of current as a pointer. When you assign it to another variable, you're copying the reference, not the value. It took me to 4th year college for someone to explain it in detail to me so I won't dive in here. Suffice it to say, using getValue() can get you out of a lot of weird spots - copying sys_ids inside a loop is the most common.


Thank you,



I will give it a go.



Cheers,


Dan


did you get a solution for this ?


sushpatil
Tera Contributor

I have also similar requirement to add in a glide_list field value on the change form to populate the affected CIs related list.
so i have created a before (insert-Update) BR with condition Field name Changes

 

 


(function executeRule(current, previous /*null when async*/ ) {


if (!previous.field_name.nil() || !previous.field_name.changes())
removePreviousCI();

if (!current.field_name.nil())
addCurrentCI();
function removePreviousCI() {
var arr = [];
var str = previous.field_name.toString().split(',');
arr.push(str);
var tam = arr.length;
for (var i = 0; i < tam; i++) {

var receda = new GlideRecord('cmdb_ci');
receda.addQuery("sys_id", arr[i]);
receda.query();
while (receda.next()) {
var service = receda.getUniqueValue();
// 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', service);
rec.query();
while (rec.next())
rec.deleteRecord();
}
}
}

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

var arr = [];
var str = current.field_name.toString().split(',');
arr.push(str);
var tam = arr.length;
for (var i = 0; i < tam; i++) {
var reced = new GlideRecord('cmdb_ci');
reced.addQuery("sys_id", arr[i]);
reced.query();
while (reced.next()) {
var service = reced.getUniqueValue();
var rec = new GlideRecord('task_ci');
rec.initialize();
rec.task = current.sys_id;
rec.ci_item = service;
rec.insert();
}

}
}
})(current, previous);

Thank you!