Show cases rows only to the assignment group

JohnDF
Mega Sage

Hi,

Is there a way to define that only the assignment group could see her cases rows?

I tryed a before BR on case table, but it not working like on incident table and I don't know why.

 

JohnDF_0-1702495788010.png

 

 

Advanced Condition is: gs.getSession().isInteractive() 

 

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

	// Add your code here
	var myUserObject = gs.getUser();


      var myUserGroups = myUserObject.getMyGroups();


      var groupsArray = new Array();


      var it = myUserGroups.iterator();


      var i=0;


      while(it.hasNext()){


              var myGroup = it.next();


              groupsArray[i]=myGroup;


              i++;


      }



var qc = 'assignment_group.sys_idIN'+groupsArray;


current.addEncodedQuery(qc);

})(current, previous);

Why is this code not working on cases?

 

Thanks for help!

1 ACCEPTED SOLUTION

malaviya
Kilo Expert

Try writing an ACL instead of BR.

View solution in original post

6 REPLIES 6

Ethan Davies
Mega Sage
Mega Sage

Try removing the condition from your Business Rule, or at least moving it to an If statement within the code block, and only execute if the condition of gs.isInteractive() is met. Conditions are generally not supported with before query BRs.

 

https://www.servicenow.com/community/developer-forum/before-query-business-rule-does-not-work-with-a...

 

Your code works fine when run in isolation. Have you verified that your code does not contain any scope-related issues? Run it as a background script in the customer service scope.

 

This is also something that you don't need to script. You could use something like Data Filtration to deny access to Cases for users who aren't in the same groups. However , you would get the 'Security Constraints' message since Data Filtration runs after the query has already been returned from the database.

Hi @Ethan Davies thanks for reply,

but removed condition and the user still can see all cases.

 

when I run the code in background it throws the error:

Evaluator: com.glide.script.RhinoEcmaError: Cannot convert null to an object.
   script : Line(39) column(0)
     36: var qc = 'assignment_group.sys_idIN'+groupsArray;
     37: 
     38: 
==>  39: current.addEncodedQuery(qc);
     40: 	//}
     41: })(current, previous);

 Business rule is created in customer service scope. Dont know what else I can do.

'Current' will not exist in a background script, since 'current' is the context of the record that is being updated that is triggering the Business Rule. To run it in a background script, just run the following modified version of your code.

var myUserObject = gs.getUser();
var myUserGroups = myUserObject.getMyGroups();
var groupsArray = new Array();
var it = myUserGroups.iterator();
var i=0;
while(it.hasNext()){
    var myGroup = it.next();
    groupsArray[i]=myGroup;
     i++;
}
var qc = 'assignment_group.sys_idIN'+groupsArray;
gs.print(qc);

Also - double-check if any other Before Query Business Rules are running on your Case table (or Parent table) that may be contradicting your query.

@JohnDF I've taken another look at your issue and the source of your problems seem to be the 'assignment_group.sys_idIN' query. It doesn't seem like you can perform that operation, i.e. Dot-walk is one of. With that in mind, try building the query with the code below instead, or something similar to it. You will essentially need to dynamically add an 'OR' condition to your Assignment Group query each time you want to consider a new one.

 

restrictCases();

function restrictCases() {
    if (gs.getSession().isInteractive()) {
        var myUserObject = gs.getUser();
        var myUserGroups = myUserObject.getMyGroups();
        // Quickly set the groups we returned to an array.
        myUserGroups = myUserGroups.toArray();
        var encodedQuery = 'assignment_group=';
        var arrayLength = myUserGroups.length;
        // Loop through the groups array, build our encoded query
        for (var i = 0; i <  arrayLength; i++) {
            if (i == 0) {
                // First element, so we treat it slightly differently to the rest
                encodedQuery = encodedQuery + myUserGroups[i];
            }  else {
                gs.print(myUserGroups[i]);
                encodedQuery = encodedQuery + '^ORassignment_group=' + myUserGroups[i];
            }
        }
        current.addEncodedQuery(String(encodedQuery)); 
    }
}