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

Hi,

Thank you for getting back to me. So I have added the above script to the Business Rule:

Business Rule Details:

Table - Incident

Runs on Update

When to run = Component Ci(s) IS NOT EMPTY


Script:

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

var componentCIs = current.u_componenet_ci.split(',');

var taskCi = new GlideRecord('task_ci');

for(var i=0; i< componentCIs.length; i++){
taskCi.initialize();
taskCi.task = current.sys_id;
taskCi.ci_item = componentCis[i];
taskCi.insert();
}

})(current, previous);

 

When I run the action on the Incident form and click save no record is added to Affected Ci tab:

find_real_file.png

OK so there was a slight spelling mistake in the script

 

var componentCIs = current.u_componenet_ci.split(',');

 

However I have corrected and still the Affected Ci record is not being created

I think what I want to point out here is the entry in the Component Ci field is not the sys_id of the configuration item but instead the sys_id of the relationship record.

I would like the script to look inside the relationship record at the CHILD field and then use this to create the relationship record and populate the ci_item field.

 

I hope this all makes sense

yep, saw that, see my reply to my initial comment with the updated script, give that a go and let me how it goes.

Hi David,

This has worked perfectly. Thank you so much

Is there something I can add to the script to do the following:

 

1) If the component Ci already exists in the Affected Ci list - Don't create another record on the task_ci table

2) If the Component Ci is removed from the Component Ci field the entry in the Affectc Ci table (record) will be removed