web
You’re offline. This is a read only version of the page.
close
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

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

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi david thorn,

    This article explains how to do this.

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Community Member Profile Picture
    on at

    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
    gdas Profile Picture
    50,091 Moderator on at

    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
    on at

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

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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
    on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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
        };
    }


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans