The CreatorCon Call for Content is officially open! Get started here.

How to restrict access via Before Query Business Rule?

SNOW Developer6
Tera Contributor

Hi,

I am working on Before Query Business Rule.

I want to show the requested_for records to  requested_for user who is logged in.

 

I have written the following code but its not working.I am only getting blank page.

 

Can u please look at it ?

 

(function ExecuteRule(current,previous)
{   
         if(gs.hasRole('myrole') && gs.getSession().isInteractive())
{
           current.addQuery('requested_for','==',gs.getUserID());
}

})(current,previous);

 

I dont know why its not working.

1 ACCEPTED SOLUTION

Cris P
Tera Guru

Hello, please try as below (remove the '==')

(function ExecuteRule(current,previous){   
         if(gs.hasRole('myrole') && gs.getSession().isInteractive()){
           current.addQuery('requested_for', gs.getUserID());
}
})(current,previous);

View solution in original post

7 REPLIES 7

Hi there, glad to hear it is working now!

gs.hasRole will always return true if the user has the admin role...

As far as I am aware a 'hasRoleExactly' method does not exist server side...

Please check the answer on this post to create your own role checker (I have implemented the same and it works):

https://community.servicenow.com/community?id=community_question&sys_id=d8edab96dbd67f80d58ea345ca96195e

 

Modify the if condition of your previous business rule as below. No need for 2 business rules.

(function ExecuteRule(current,previous){   
         if((gs.hasRole('myrole') && !gs.hasRole('admin'))&& gs.getSession().isInteractive()){
         current.addQuery('requested_for',gs.getUserID());
}
})(current,previous);

I will again suggest you to use condition field of business rule. See the below image for reference.

find_real_file.png