- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 02:45 AM
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
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
I hope it is clear what I am trying to do. Any assistance would be most appreciated
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2018 02:30 AM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 03:08 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 03:11 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 03:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 03:23 AM
yep, saw that, see my reply to my initial comment with the updated script, give that a go and let me how it goes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 09:30 AM
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