Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Hiding button based on security role is giving unexpected experience (Ribbon Workbench)

Jump to Best Answer
  • 1.  Hiding button based on security role is giving unexpected experience (Ribbon Workbench)

    GOLD CONTRIBUTOR
    Posted Jan 15, 2019 12:05 PM
    I am attempting to hide the CLOSE AS WON and CLOSE AS LOST buttons from the Opportunity form ribbon based on the user's security role names.
    For example, Level 2, 3, and System Administrator will see the buttons. Level 1 should not see the buttons. I am looking for feedback on what could be the issue.

    I am using the following javascript to get the user role names. A variable (rolesOutputText) is populated with the names of the roles assigned to the user.

    function getUserRoles() {
        // Get Logged In User Context
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
        // Get Logged In User Security Roles
        var arr_roleid = userSettings.securityRoles;
        var arr_totalSecurityRoles = [];
        var rolesOutputText = "";
    
        if (arr_roleid.length > 0) {
            Xrm.WebApi.retrieveMultipleRecords("roles", "?$select=name,roleid").then(
                function success(result) {
                    if (result.entities.length > 0) {
                        // Push Role Names and Role Ids to Array
                        for (var rolesCount = 0; rolesCount < result.entities.length; rolesCount++) {
                            arr_totalSecurityRoles.push({
                                RoleName: result.entities[rolesCount].name,
                                RoleId: result.entities[rolesCount].roleid
                            });
                        }
    
    
                        // Compare the User Security Roles with Total Security Roles
                        for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < arr_roleid.length; userSecurityRolesCounter++) {
                            for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < arr_totalSecurityRoles.length; totalsecurityRolesCounter++) {
                                if (arr_totalSecurityRoles[totalsecurityRolesCounter].RoleId.toLowerCase() == arr_roleid[userSecurityRolesCounter].toLowerCase()) {
                                    rolesOutputText += arr_totalSecurityRoles[totalsecurityRolesCounter].RoleName + "\n";
    								break;
                                }
                            }
                        }
                    }
    
                    // Show User Roles
    				if(rolesOutputText.includes("System Administrator") == true || rolesOutputText.includes("Level 2") == true || rolesOutputText.includes("Level 3") == true){
    					return true;
    					}
    					else {
    						return false;
    					}
    				},
                function(error) {
                    // Return false
                    return false;
                });
        }
    }


    Then in ribbon workbench, I create a custom Enable Rule:
    Default: False
    Invert Result: <blank>
    FunctionName: getUserRoles
    Library: $webresource:abc_getUserRoles

    I add this enable rule to the commands for CLOSE AS WON and CLOSE AS LOST, and I publish.

    Opening the form shows that the buttons are hidden for ALL users, no matter their permissions.

    #intermediate #customization #ribbonworkbench

    ​​​

    ------------------------------
    Adam Posegate
    Fox Valley Technical College
    Appleton WI
    ------------------------------


  • 2.  RE: Hiding button based on security role is giving unexpected experience (Ribbon Workbench)
    Best Answer

    GOLD CONTRIBUTOR
    Posted Jan 15, 2019 12:16 PM
    Hello,
    This is an expected result because:
    1. Xrm.WebApi does all the calls in async way so your getUserRoles function ends before retrieve operation is completed.
    2. You function doesn't return anything other than from promise callbacks so... it returns null that is not equal to true so buttons are hidden.

    I had scenario similar to yours so I developed code that will do what you need. You can find the code here - https://butenko.pro/2018/11/13/showing-ribbon-button-based-on-the-result-of-async-operation/

    ------------------------------
    Andrew Butenko
    ------------------------------



  • 3.  RE: Hiding button based on security role is giving unexpected experience (Ribbon Workbench)

    GOLD CONTRIBUTOR
    Posted Jan 15, 2019 01:30 PM
    Thank you - that did it.

    ------------------------------
    Adam Posegate
    Fox Valley Technical College
    Appleton WI
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role