Including validation within an if and obtaining the group name to generate an alert message

Igor Corr_a
Tera Expert

Hello,

I am creating a Business Rule to block the use of the "Copy partial project" and "Copy project" options via the planning console.

Despite already having an ACL that controls the creation of new projects, the options are still available within the planning console and I chose not to change the Angular that makes these buttons available.

The most viable option would be the creation of a BR.

Scenario: Only members of a certain group can create new projects.

With the current implementation of BR, users who do not belong to this group receive the access denied message when proceeding with the creation of the new project, however, I would like to improve this feedback.

Currently, my Business Rule is implemented like this:

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

var check = gs.getUser().getUserByID(current.sys_id).isMemberOf(gs.getProperty('nomeOfMyPropertyContainingSysIDOfMyGroup'));

if(check == 1){
gs.addErrorMessage(gs.getMessage("Group members only") + " " + ("Here I would like to include the name of my group") + " " + ("you can copy projects"));
current.setAbortAction(true);
}

})(current, previous);

Part 1: I believe that my if is not being executed because the access denied message continues to appear and not my personalized message.

Part 2: How to get the name of my group within the alert message informing the sys_id because eventually the group name may change and I would not like to leave the name recorded in the code.

Best Regards,

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@Igor Corr_a Please update your script as follows and see if it works for you.

 

 

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

var groupSysID = gs.getProperty('nomeOfMyPropertyContainingSysIDOfMyGroup'); //Replace with your property name
var glideGroup = new GlideRecord('sys_user_group');
var groupName='';
if(glideGroup.get(groupSysID)){
groupName=glideGroup.getValue('name');
}

if(!gs.getUser().isMemberOf(groupName)){ //isMemberOf only accepts group names and not sys_id
gs.addErrorMessage(gs.getMessage("Group members only") + " " + groupName + " " + gs.getMessage("can copy projects"));
current.setAbortAction(true);
}

})(current, previous);

 

Hope this helps.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Igor Corr_a ,
I trust you are doing great.
Please find the corrected code

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

    var user = gs.getUser();
    var groupNameProperty = gs.getProperty('nomeOfMyPropertyContainingSysIDOfMyGroup');
    
    if (user.isMemberOf(groupNameProperty)) {
        // Fetch the group name dynamically
        var grGroup = new GlideRecord('sys_user_group');
        if (grGroup.get(groupNameProperty)) {
            var groupName = grGroup.name;
            gs.addErrorMessage(gs.getMessage("Group members only") + " " + groupName + " " + gs.getMessage("can copy projects"));
            current.setAbortAction(true);
        }
    }

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Igor Corr_a
Tera Expert

Hi @Sandeep Rajput  and @Amit Gujarathi,

Thank you both for your feedback.

I tested both solutions, however, the access denied message displayed in the planning console when confirming the total or partial copy of the project displayed on the screen remains.

Is this message coming from the frontend of the planning console without going through BR despite BR validating whether the user is a member of the group or not?

@Igor Corr_a Could you please post the screenshot of the error you are viewing.