Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

show/Hide button bases on different criteria

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello expert ,

I need to show/Hide a personnalized button i have created with ribbonworkbench tool based on a different criteria(security role, status)

so let's say that i want to hide my button if a record is inactive & the user is a commercial(security role) & i want to show the same button for a record which is active( the user is a commercial) or where the user is a marketing director & the record is inactive.

Thank you in advance,

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: show/Hide button bases on different criteria

    Hi David ,

    You should refer the code what Andrew Shared in the first link - 

    Here I copied Andrew's code and replace your logic ,you can try  to replace the function name RibbonShowHide in the enable rule -

    function RibbonShowHide() {
     
        //this variable stores if async operation was already completed
        var isAsyncOperationCompleted = false;
        //this variable stores the result - if button enabled or not
        var isButtonEnabled = false;
     
        function IsButtonEnabled(formContext) {
            //If async operation was already completed I just return the result of it
            if (isAsyncOperationCompleted) {
                return isButtonEnabled;
            }
     
            //getting of userid from the context
            var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace("{", "").replace("}", "");
     
            //fetching user by id and expanding roles to get role names
            Xrm.WebApi.retrieveRecord("systemuser", userId, "?$expand=systemuserroles_association($select=name)").then(
                function success(result) {
                    //Async operation was completed successfully
                    isAsyncOperationCompleted = true;
     
                    //looping through all the roles available for user
                    for (var i = 0; i < result.systemuserroles_association.length; i++) {
                        //getting current role name
                        var roleName = result.systemuserroles_association[i]["name"];
     
                        //if role name is equal to "System Administrator" - setting variable to true
                        if (roleName === "Sales Manager") {
                            isButtonEnabled = true;
                        }
                    }
     
                    //so if role is there - just refresh the ribbon to see the button
                    if (isButtonEnabled) {
                        formContext.ui.refreshRibbon();
                    }
                },
                function (error) {
                    //if something went wrong during the data retrieval
                    //operation is marked as completed and error message is shown
                    isAsyncOperationCompleted = true;
                    Xrm.Navigation.openAlertDialog({ text: error.message });
                }
            );
     
            //Just a stub that hides button by default
            //if role is available for a user - refresh of ribbon will unhide the button
            return false;
        }
     
        return {
            IsButtonEnabled: IsButtonEnabled
        };
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: show/Hide button bases on different criteria

    Thank you for the explanation , but i still cannot got an issue.

    1. Js function which return true/false based on my criteria: 

    function isButtonEnabled() {
    // Get Logged In User Context
    var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    // Get Logged In User Security Roles
    var loggedInUsersecurityRolesGuidArray = userSettings.securityRoles;
    var totalSecurityRolesArray = new Array();
    var rolesOutputText = "";

    var recordstatus=Xrm.Page.getAttribute("statecode").getValue();

    if (loggedInUsersecurityRolesGuidArray.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++) {
    totalSecurityRolesArray.push({ RoleName: result.entities[rolesCount].name, RoleId: result.entities[rolesCount].roleid });
    }

    rolesOutputText = userSettings.userName + " has the below Security Roles\n------------------------------------\n" ;

    // Compare the User Security Roles with Total Security Roles
    for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < loggedInUsersecurityRolesGuidArray.length; userSecurityRolesCounter++) {
    for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < totalSecurityRolesArray.length; totalsecurityRolesCounter++) {
    if (totalSecurityRolesArray[totalsecurityRolesCounter].RoleId.toLowerCase() == loggedInUsersecurityRolesGuidArray[userSecurityRolesCounter].toLowerCase()) {
    if(totalSecurityRolesArray[totalsecurityRolesCounter].RoleName=="Sales Manager" && recordstatus==0 )
    {
    isButtonEnabled=true;
    rolesOutputText += totalSecurityRolesArray[totalsecurityRolesCounter].RoleName + "\n";
    break;
    } else isButtonEnabled=false;
    }
    }
    }
    }
    Xrm.Page.getAttribute("new_hide_show").setValue(isButtonEnabled);
    // Show User Roles
    Xrm.Utility.alertDialog(isButtonEnabled+""+Xrm.Page.getAttribute("new_hide_show").getValue() ,null);
    },
    function (error) {
    // Show error
    Xrm.Utility.alertDialog(error.message, null);
    });
    }

    return isButtonEnabled;
    }

    2.create a button ,add a command to this button & then add a enable rule.

    8611.command1.PNG     8611.command1.PNG

    enabrule.PNG

  • Verified answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: show/Hide button bases on different criteria

    No , both enable /display rule show/hide the button , and none of the rule activate or deactivate button. The name is given is confusing so its not your mistake.

    The only difference is that using enable rule the action will take place after HTML DOM loaded but for display rule action will take place before HTML DOM  load and thats why there is no JavaScript rule in display rule.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: show/Hide button bases on different criteria

    Thank you Goutam, but enable rule is for Activate/Desactivite button ,no?

  • Verified answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: show/Hide button bases on different criteria

    Hi David ,

    You need to create enable rule, display rule does not  support JavaScript rule as display rule execute in server side.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: show/Hide button bases on different criteria

    thank you Andrew , i create the function that return true or false based on my criteria.(works fine)

    then i created a command & add a display rule to it .but the problem is that i cannot find the custom js option in the display rule.

    6153.t2.PNG

    6153.t2.PNG

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: show/Hide button bases on different criteria

    Hello David,

    I wrote a post about similar requirement a week ago - butenko.pro/.../showing-ribbon-button-based-on-the-result-of-async-operation

    It looks like it's close to yours usecase but to use it you will have to rewrite code a bit.

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,019 Super User 2025 Season 1 on at
    RE: show/Hide button bases on different criteria

    Hi david thorn,

    This article explains how to do this.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,399 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans