How to execute query of a different table in a Query business rule?

Mohd Asad Khan2
Tera Contributor

I have a Query business rule (on sc_req_item table) that restricts visibility of RITM records. I want to add a condition to that business rule to allow the users to view the RITM records that they are approvers of. The sc_req_item field does not have a Approver(s) field that I can use in the query condition. Also, approval records are stored in a different table, sysapproval_approver. How to check if the current logged in user is an approver of the current RITM and then grant permission. We already have an ACL that grants approvers the read access, but the Query business rule is restricting access, so I want to add some logic inside the Query business rule only.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

This script in a before Query Business Rule on the sc_req_item table will cause only those RITMs where the current user is an approver to appear in the list:

(function executeRule(current, previous /*null when async*/) {
	
	var answerArr = [];
	var app = new GlideRecord('sysapproval_approver');
	app.addQuery('approver', gs.getUserID());
	app.query();
	while (app.next()){
		answerArr.push(app.sysapproval.toString());
	}
	
	current.addQuery('sys_id', 'IN', answerArr.join(','));
	
})(current, previous);

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

This script in a before Query Business Rule on the sc_req_item table will cause only those RITMs where the current user is an approver to appear in the list:

(function executeRule(current, previous /*null when async*/) {
	
	var answerArr = [];
	var app = new GlideRecord('sysapproval_approver');
	app.addQuery('approver', gs.getUserID());
	app.query();
	while (app.next()){
		answerArr.push(app.sysapproval.toString());
	}
	
	current.addQuery('sys_id', 'IN', answerArr.join(','));
	
})(current, previous);

Thanks for your response, Brad. It was helpful.

You are welcome.