Before 'Query' Business rule

Ben42
Tera Contributor

Hello,

 

I have created a below 'Before-Query' business rule on Task table to restrict visible access to users with custom role where users with custom role will see only records that are assigned to one of their groups.

 

Ben42_0-1671209439325.png

 

But, I want to have an encoded query for incident table like if "assignment group is one of my groups" and if "caller_id is me". How can I compensate the new encoded query in the same business that should work for incident table only since caller_id field is only on incident table.

 

Any help is appreciated.

 

Thanks,

Rocky.

 

5 REPLIES 5

Ben42
Tera Contributor

Hello,

 

Thanks for the response. So, I will try to make it simple. in the screenshot I provided I have the encoded query that works on entire task table, So now I wanted to have a separate new encoded query that should work only for Incident table. So, should I create new BR for incident table or is there a way I compensate in the same BR?

 

Ben42_0-1671211814300.png

 

Ben42_1-1671211848645.png

 

 

Thanks,

Rocky.

Ben42
Tera Contributor

Hello,

 

Yes, we have sys_class_name but, caller_id field is not on Task table right, it is on Incident table and my encoded query will include caller_id field.

 

Thanks,

Rocky.

Ben42
Tera Contributor

Hey,

 

I tried the below code but it is not working. with the below script when I open incident.list the script 'else' condition is being executed.

 

But, when I open Incident.list the script in 'if' condition should be executed, And if I open any other table that extends Task table the script in 'else' condition should be executed.

 

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

if (current.sys_class_name == 'incident') {
current.addEncodedQuery("ref_incident.caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe^ORassignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744");
} else {
current.addEncodedQuery("assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744");
}

})(current, previous);

 

Thanks,

Rocky.

jaheerhattiwale
Mega Sage
Mega Sage

@Ben42 You can use below code to add the query for incident in same business rule.

 

if(current.sys_class_name == "incident"){
    var myGroups = [];
    var groupMembers = new GlideAggregate("sys_user_grmember");
    groupMembers.addQuery("user="+gs.getUserID());
    groupMembers.groupBy("group");
    groupMembers.query();

    while(groupMembers.next()){
        myGroups.push(groupMembers.group.toString());
    }

if(myGroups.length > 0){
    current.addQuery("assignment_groupIN"+myGroups.toString());
}
current.addQuery("caller_id="+gs.getUserID());
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023