Incident Form - Affected Ci Update

danielbartholom
Mega Expert

HI,

 

Looking for some assistance please:

On my incident form I have a custom field called Component Ci(s) (type List - referencing cmdb_rel_ci) basically this field is present so any Configuration item that has a relationship with the Technical System (also present on the Incident form) is selectable. Please see screen shot below

 

find_real_file.png

 

As you can see from the screenshot when you click on the i icon next to the Component Ci selected, the Relationship record displays.

What I am looking to do is write a Business Rule to initialize a record on the task_ci table (Affected Ci) so the Child of the relationship record is written to the ci_item column on the task_ci table.

Here is what I have tried but I don't think the scripting is correct:

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

var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('sys_id', current.u_component_ci);
rel.addQuery('type', '47e4a6424fcb5640c9ca69d18110c7bf');
rel.query();

var taskci = new GlideRecord('task_ci');
taskci.initialize();

taskci.ci_item.setDisplayValue(rel.child);
taskci.setValue('task', current.sys_id);
taskci.insert();

})(current, previous);

 

Right now when this script runs a record is created on task_ci as you can see below but the ci_item is not being populated correctly

find_real_file.png

I hope it is clear what I am trying to do.  Any assistance would be most appreciated

Thanks

 

1 ACCEPTED SOLUTION

The below script will clear the relationships for the current task and then repopulate with the ones in the component CI field.

var gr = new GlideRecord('task_ci');
gr.addQuery('task', current.sys_id);
gr.query();
gr.deleteMultiple();

var componentCI = current.u_component_ci.split(',');

for(var i=0; i< componentCI.length; i++){

var rel = new GlideRecord('cmdb_rel_ci');
if(rel.get(componentCI[i])){

var taskci = new GlideRecord('task_ci');
taskci.initialize();
taskci.ci_item = rel.getValue('child');
taskci.task = current.sys_id);
taskci.insert();
}
}

View solution in original post

12 REPLIES 12

The below script will clear the relationships for the current task and then repopulate with the ones in the component CI field.

var gr = new GlideRecord('task_ci');
gr.addQuery('task', current.sys_id);
gr.query();
gr.deleteMultiple();

var componentCI = current.u_component_ci.split(',');

for(var i=0; i< componentCI.length; i++){

var rel = new GlideRecord('cmdb_rel_ci');
if(rel.get(componentCI[i])){

var taskci = new GlideRecord('task_ci');
taskci.initialize();
taskci.ci_item = rel.getValue('child');
taskci.task = current.sys_id);
taskci.insert();
}
}

David,

 

You are simply a wizard my friend. Thank you so much this is working great. I really appreciate your time working on this with me.

 

Cheers

 

Dan

You're welcome 🙂