Deleting cmdb_rel_ci records through a Business Rule

David Casper
Tera Guru

I've successfully created a BR that runs on a specific CI service table to create a specific relationship between the current CI and the parent. This is triggered when the parent field changes AND is not empty. 

I'm not trying to create the same rule, but in reverse. So when a parent field is removed (changes AND is empty) it deletes the relationship that existed. 

The BR runs and I'm able to get it to delete the when I specify a specific name of a parent and child. However, when I try it as below it does not work. It appears to be an issue with not pulling in the 'current' records values, but for some reason I'm stuck. I'm sure it will be something silly I overlooked so thanks in advance!

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

var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('parent.name',current.parent.name);
rel.addQuery('child.name',current.name);
rel.query();

while(rel.next())
{
rel.deleteRecord();
}



})(current, previous);

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

Here's an example script that should help you do this.  It's designed to run in an 'after' business rule when the current.parent changes...

if(!previous.parent.nil()){
    //Query for the CI relationship to parent
    var rel = new GlideRecord('cmdb_rel_ci');
    rel.addQuery('child', current.sys_id);
    rel.addQuery('parent', previous.parent.sys_id);
    rel.query();
    if(rel.next()){
        rel.deleteRecord();
        gs.addInfoMessage('CI Relationship to ' + previous.parent.getDisplayValue() + ' removed.');
    }
}

 

View solution in original post

4 REPLIES 4

Mark Stanger
Giga Sage

Here's an example script that should help you do this.  It's designed to run in an 'after' business rule when the current.parent changes...

if(!previous.parent.nil()){
    //Query for the CI relationship to parent
    var rel = new GlideRecord('cmdb_rel_ci');
    rel.addQuery('child', current.sys_id);
    rel.addQuery('parent', previous.parent.sys_id);
    rel.query();
    if(rel.next()){
        rel.deleteRecord();
        gs.addInfoMessage('CI Relationship to ' + previous.parent.getDisplayValue() + ' removed.');
    }
}

 

That seems to have worked. Thanks a ton! Also nice job on the guru site! It's been very beneficial.

You're welcome!

I replied to this thread - https://community.servicenow.com/community?id=community_question&sys_id=60d40fe9dbd8dbc01dcaf3231f961998

Noticed you hadn't been on there in a while so wanted to make sure you saw it. It's quite old. It continues on with this topic.