
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2018 01:27 PM
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);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2018 01:36 PM
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.');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2018 01:36 PM
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.');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2018 07:30 AM
That seems to have worked. Thanks a ton! Also nice job on the guru site! It's been very beneficial.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2018 08:45 AM
You're welcome!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2018 11:28 AM
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.