The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Update parent record same as value on child (cmdb_rel_ci)

GJones
Tera Contributor

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?

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

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

 

View solution in original post

2 REPLIES 2

Tony Chatfield1
Kilo Patron

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

 

ahh! Like magic. Thank you so much.