Business rule not updating the relationship type

Sumit Raj
Kilo Explorer

Hi Experts,

I am writing one after business rule on cmdb_ci_service_discovered table to update relationship type in cmdb_rel_ci table.

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

    // Add your code here
    var rel = new GlideRecord('cmdb_rel_ci');
    {
        rel.addQuery('child', current.sys_id);
        rel.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');
        rel.query();
        if(rel.next())
            {
                gs.log("test" + rel.getDisplayValue('type'));
                rel.type = '41008aa6ef32010098d5925495c0fb94';
                rel.update();
                gs.log("demo" + rel.getDisplayValue('type'));
            }
    }

})(current, previous);

But its not updating the record in the cmdb_rel_ci table. Is it like I would have to add new record and delete the existing one to achieve this?

6 REPLIES 6

Hi Tony,

Thanks for pointing it out. I have made the corrections still its not working. Could you please let me know if there is another way to achieve the same?

Hi, I tested your code in a PDI yesterday (before I responded to your post) and I found no issues other then the spelling error, the extra {} as covered by Ankurs response and the sys_id for the type/relationship not being found in my PDI - is this custom relationship or an error?

Otherwise I used below code, first instantiating a variable called current (for testing) and setting it to the sys_id of this CI
/nav_to.do?uri=cmdb_ci_service.do?sys_id=2fcd6b5d0a0a0bb400ac2f4a7651e8d3

when run the script replaced the OOB relationship (I think it was Contains\contained_by) with depends on\used by as was expected.
/nav_to.do?uri=cmdb_rel_type.do?sys_id=1a9cb166f1571100a92eb60da2bce5c5

This is the relationship that should be updated.
/cmdb_rel_ci_list.do?sysparm_query=sys_id%3D3a314d4e0a0a0bb4003f8fc2a508d460&sysparm_view=

If you test below script in a background window in PDI you should finds it works, if you cannot get it to work in a production environment then you may need to check for BR's or ACL's that are preventing the update.

var current = new GlideRecord('cmdb_ci');
current.get('2fcd6b5d0a0a0bb400ac2f4a7651e8d3');

var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('child', current.sys_id);
rel.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');
rel.query();

if(rel.next()) {
gs.info("test" + rel.getDisplayValue('type'));

//rel.type = '41008aa6ef32010098d5925495c0fb94';
rel.type = '1a9cb166f1571100a92eb60da2bce5c5';
rel.update();
gs.info("demo" + rel.getDisplayValue('type'));
}