Database View on cmdb_rel_ci

JamesLindsay
Giga Guru

Very simply, I am trying to create a report/view of any server (cmdb_ci_server) that is not a child in (cmdb_rel_ci). Everything I've tried keeps returning the same set of servers which actually are children to some other parent.

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

I may be overthinking/overcomplicating this, but here's one way that seems to work.

Setup the view like this to show all servers, joined with the rel table so that you can grab columns from that table too:

BradBowman_0-1696444076940.png

If you don't need to see any columns from the rel table, then you can do a self-join like this, for the purpose of doing the next step which is to filter the list, while not affecting the cmdb_ci_server table system-wide.

BradBowman_1-1696444748936.png

The 'is not a child' is the tricky part on db views, so next create a Business Rule using the db view as the Table and selecting before Query on the When to Run tab.  Your script on the Advanced tab should look like this (for the first db view example - adjust prefix svr to svr1 if using second db view example):

(function executeRule(current, previous /*null when async*/) {
	var ciArr = [];
	var svrGR = new GlideRecord('cmdb_ci_server');
	svrGR.query();
	while (svrGR.next()) {
		var relGR = new GlideRecord('cmdb_rel_ci');
		relGR.addQuery('child', svrGR.sys_id);
		relGR.query();
		if (relGR.next()) {
			ciArr.push(svrGR.sys_id.toString());
		}
	}
	
	current.addQuery('svr_sys_id', 'NOT IN', ciArr.join(','));
})(current, previous);