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

Chalan B L
Giga Guru

Hello Daniel,

open Task=INC**** in new tab then you could see if there is an intermediate relationship table.

if intermediate table is present then you have to glide that first and then insert the record.

just confirm me if any relationship table is present and if possible give the screenshots.

so that i could give a quick script for this...!!!

 

Please mark the answer as correct and helpful if it helped you..!!!

 

Regards,

Chalan

Hi Chalan,

 

Thank you for getting back to me, sorry I am a little clear what you want me to do here?

 

open Task=INC**** in new tab then you could see if there is an intermediate relationship table????

Dubz
Mega Sage

Hi Daniel,

The main issue with your script is that you're not looping through the results of the glide query, you'd normally have while(gr.next()) following the gr.query() line to start a loop so you can create the records you need.

The script below should work, it's just looping through the contents of your component ci field and creating a task ci record for each, trigger it after insert/update when component ci changes.

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();
}

Actually it's just clicked that your component Ci field is showing a relationship record (not sure why you would do this?!). Script below should work:

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();
}
}