- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 07:18 AM - edited ‎02-27-2024 07:25 AM
I currently need to create a business rule that will update the parent of a child record via the cmdb_rel_ci table.
For example
I have a record on cmdb_ci_vm_instance. It is related as a child to a parent record on the cmdb_ci_linux_server table.
When the support_group field is updated on the child record (that is always on cmdb_ci_vm_instance) then it needs to update the support_group field on the parent record, which may differ in tables - not all parent records will be on the cmdb_ci_linux_server, for example.
Currently I have a business rule that runs on the cmdb_ci_vm_instance table
When: after, update
Conditions:
class is virtual machine instance
support group changes
support group is not empty
Script:
(function executeRule(current, previous /*null when async*/ ) {
var prnt = new GlideRecord('cmdb_rel_ci');
prnt.addQuery('child', current.sys_id);
prnt.addEncodedQuery('type=<sys_id of type of relationship>');
prnt.query();
while (prnt.next()) {
gs.info('child support group ' + current.support_group');
gs.info('parent support group ' + prnt.parent.support_group');
if (current.get(prnt.child)) {
var sp = prnt.parent;
gs.info('parent found');
sp.support_group = current.support_group;
sp.update();
}
}
})(current, previous);
I am successfully getting the info logs, but the parent record on the cmdb_rel_ci table do not get updated. What am I missing?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 01:54 PM
Hi, in order to update the parent record you would need to lookup the parent with GlideRecord, set the new value and then update() the record. something like this.
var prnt = new GlideRecord('cmdb_rel_ci');
prnt.addQuery('child', current.sys_id);
prnt.addEncodedQuery('type=<sys_id of type of relationship>');
prnt.query();
while (prnt.next()) {
var myParent = new GlideRecord(prnt.parent.sys_class_name);
if(myParent.get(prnt.parent)) {
myParent.targetField = prnt.child.sourceField;
myParent.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 01:54 PM
Hi, in order to update the parent record you would need to lookup the parent with GlideRecord, set the new value and then update() the record. something like this.
var prnt = new GlideRecord('cmdb_rel_ci');
prnt.addQuery('child', current.sys_id);
prnt.addEncodedQuery('type=<sys_id of type of relationship>');
prnt.query();
while (prnt.next()) {
var myParent = new GlideRecord(prnt.parent.sys_class_name);
if(myParent.get(prnt.parent)) {
myParent.targetField = prnt.child.sourceField;
myParent.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 12:04 AM
ahh! Like magic. Thank you so much.