
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2023 01:34 AM
Hello All
I have the need to hide certain business rules outside of a specific user role. So I have a True/False field in the sys_script table called "u_ebonding_business_rule".
If this field is set to true, those records can only be seen by users with the role "ebonding_admin".
I have created a query business rule with the code below but it doesn't work..
(function executeRule(current, previous /*, gs*/ ) {
// Define the name of the role that can view the business rule
var allowedRole = "ebonding_admin";
// Check if the business rule has the 'ebonding business rule' flag set to true
var ebondingFlag = current.getValue("u_ebonding_business_rule");
if (ebondingFlag == true) {
// Get the current user's role(s)
var currentUser = gs.getUser();
var userRoles = currentUser.getRoles();
// Check if the user has the allowed role
if (userRoles.indexOf(allowedRole) == -1) {
// If the user does not have the allowed role, restrict visibility
current.setDisplay(false);
gs.info("Business rule " + current.name + " hidden from non-" + allowedRole + " users.");
}
}
})(current, previous);
Can someone help me ? 🙂
Thanks
Jérôme
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2023 01:46 AM - edited ‎05-09-2023 02:37 AM
Hi @Jerome MAISETTI ,
Please try with this version of your business rule:
(function executeRule(current, previous /*, gs*/ ) {
// Define the name of the role that can view the business rule
var allowedRole = "ebonding_admin";
// Get the current user's role(s)
var currentUser = gs.getUser();
var userRoles = currentUser.getRoles();
// Check if the user has the allowed role
if (userRoles.indexOf(allowedRole) == -1) {
// If the user does not have the allowed role, restrict visibility
current.addEncodedQuery('u_ebonding_business_rule=false');
gs.info("Some Business rules are hidden for non-" + allowedRole + " users.");
}
})(current, previous);
If my answer has helped with your question, please mark it as correct and helpful
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2023 02:38 AM
Thanks @Simon Christens
@Jerome MAISETTI , I've updated the BR script, please use that
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2023 01:50 AM - edited ‎05-09-2023 01:54 AM
Hi
Query business rules doesnt work like that.
query business rules applies a filter - so you do not have access to "current" there
What needs to be done is something like:
if(!gs.getUser().hasRole('ebonding_admin')){
current.addQuery('u_ebonding_business_rule', false);
}
You could even add the role check in the condition field of the business rule so it only runs if users doent have the ebonding_admin role and then apply the current.addQuery() part in the script
This way you enforce an extra filter condition for users that doesnt have the ebonding_admin role
This is from the docs on when BRs runs