Before Query Business Rule and Related List

Jamsta1912
Tera Guru

Hello all,

I'm trying to create a Before Query Business rule on the incident table to restrict some users' list view to include only those incidents raised against a given company (e.g. ACME China), plus incidents with an affected location that has company = ACME China.

Ideally I wanted to do this with an encoded query, by combining these two query strings:

var qStr1 = 'company=81fdf9ebac1d55eb4cb89f136a082555';

var qStr2 = '^RLQUERYtask_location.task,>=1,m2m^location.company=81fdf9ebac1d55eb4cb89f136a082555^ENDRLQUERY';

var qStr3 = qStr1 + '^NQ' + qStr2;

current.addEncodedQuery(qStr3);

But this doesn't work with related list queries - I just get the results for qStr1.

So another thing I tried was to separately create a list of sys_ids for those incidents that would have been returned by qStr2:

var taskList = '';
var tsklocGR = new GlideRecord('task_location');
tsklocGR.addQuery('location.company', '81fdf9ebac1d55eb4cb89f136a082555');
tsklocGR.query();
while(tsklocGR.next()){
  var tsk = tsklocGR.getValue('task');
  if (!taskList.includes(tsk))
      taskList += tsk + ',';
}

var qStr = 'company=81fdf9ebac1d55eb4cb89f136a082555^ORsys_idIN' + taskList;

current.addEncodedQuery(qStr);

That second approach works, in the functional sense, but it takes a very very long time to run as I think taskList is created and compared individually against every incident that would otherwise be returned in the list view.

Does anyone have any ideas how best to achieve what I'm after? (Unfortunately domain separation is NOT an option right now).

Thanks

Jamie.

2 REPLIES 2

Brent Sutton
Mega Sage

Instead of using a business rule why don't you restrict access to those records using READ Access control list rules (ACL's)? ACL's will prevent those records from being visible in any view.

More information can be found on ServiceNow docs - Access control list rules

Let me know if that worked for you.

Brent

Hi Brent,

I had thought about ACLs, but I realised I would have the same issue: I would still need a script on the ACL to run a GlideRecord lookup against the task_location table.

But I've come up with a different solution. I'm going to add a new list field to the incident table to hold a list of the companies with affected locations, and populate that field dynamically with business rules whenever an affected location is added or removed. And then, I can reference that new field in my before business rule query, so someting like: company = ACME China, or u_company_list CONTAINS ACME China. 

Jamie