Showing posts with label Ribbon. Show all posts
Showing posts with label Ribbon. Show all posts

Friday, November 11, 2022

Hide/Show OOB Activate and Deactivate Ribbon Button Based on Security Role D365

 Step By Step Process to hide/show Ribbon button based on security role using javascript

Hide the OOB Activate and Deactivate ribbon buttons on the D365 Form/Views based on the user security role in Dynamics 365.
On the form Activate/Deactivate Ribbon Button:



  • Create a JavaScript function that returns true or false based on whether the user has the Salesperson security role. You will see the JavaScript below:
    • False will hide the buttons
    • True will show the buttons
  • Put this function in the Contact JavaScript Library web resources. This makes it available in the ribbon and determines if the buttons should be visible.
  • Edit the buttons in the Ribbon Workbench to use the new function.

Now, let's take a look at the steps to make this happen.

1. Create JavaScript

A - Open or create a web resource to hold the JavaScript function.
B - For our purposes, the web resource is stn_ContactJavaScriptLibrary
C - Paste the following JavaScript into the web resource: 

hideActivateAndDeactivateRibbonButtons: function () {
            var value = true;
            var constSalesPersonGuid = "74E3A3A3-E6F3-4B97-A045-A557B1660293";
            var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
            for (var i = 0; i < userRoles.length; i++) {
                if (userRoles[i].toUpperCase() == constSalesPersonGuid .toUpperCase()) {
                    value = false;
                    break;
                }
            }
            return value;

        },

2 - Create Enable Rule in Ribbon Workbench

A -  Create a solution with the Contact table
B - Open solution with Ribbon Workbench
C - Right-click on Activate button

C1 - Select Customize Command


C2 - Click + next to Enable Rules


C3 - Click Add Step


C4 - Click CustomRule

C5 - Enter the function name from the Contact JavaScript Library
In this case, it is: hideActivateAndDeactivateRibbonButtons
C6 - Click the magnifying glass and select the Contact JavaScript Library

C7 - It should look like this when you are done with this step:



D - Click on the Command and then click Add Enable Rule

E - Click on the Enable Rule you created previously 


3 - For the Deactivate Button

A - Right-click on the Deactivate Button and Select Customize Command
B - Since the enable Rule already exists, we don't need to perform that step

C - Click on Command, and then click Add Enable Rule



D - Click on the Enable Rule you previously created

4 - Click Publish in the Ribbon Workbench


5 - Test it out!

You are now ready to give this solution a test before going live. Just follow these steps:

A - Look at the Contact Ribbon for a user who does not have the Salesperson security role. You will notice the Deactivate button. 


B - Add the Salesperson security role to your user

C - Go back to the Contact and do a hard refresh of the browsers tab and there should be no Deactivate button. 



D - Test the activate button by: 
D1 - Removing the Salesperson role from the user
D2 - Doing a hard Refresh of the Contact tab
D3 - Check to see if the Deactivate button is visible on the Contact
D4 - Click the Deactivate button
D5 - Now you should see the Activate button
D6 - Add the Salesperson role to the user
D7 - Do another hard refresh of the Contact tab

D8 - And now the Activate button is no longer visible.
Note: To hide Activate/Deactivate button on View - Add the same enable rule as how it's done for form.

Monday, April 5, 2021

Step By step process to Add Ribbon button and onclick open Modal Dialog Form

Start your work with Ribbon Workbench:

Follow the step-by-step process to add a Ribbon Button on the entity form and perform Create and update operations with the click of the ribbon button.

In this blog, I'm going to show you step by step process:
1. To add a button on Ribbon (let's say Update) on the email activity form.
2. Onclick of Update button opens a new record form 
in the pop-up window in Modal Dialog Form if the record doesn't match with the record ID on the email activity form (Lookup Field: "to").
3. Else OnClick open update form in the pop-up window in Modal Dialog Form if record exists with the record ID on the email activity form (Lookup Field: "to").


Click here to learn more about Xrm.Navigation.navigateTo

Note: A new feature in the 2020 Release Wave 1 enables users to open records in a modal dialog without leaving the main record form in model-driven apps for Dynamics 365 and Power Apps.

Here are few more references related to the dynamics 365 ribbon button: 
To hide and show button on dynamics 365 ribbon.

----------------------------------------------------------------------------------------------------------
Let's begin: 

1. First Create a basic javascript file (email.js), add method (OnclickOfRibbonButton), and logic in it. Which can be used on the ribbon button click functionality.

Here you start, follow the example below add below code in your email.js webresource file:

if ("undefined" == typeof (NEW)) {
    NEW = { __namespace: true };
}
 
NEW.Email = {
    OnclickOfRibbonButton: function (primaryControl) {
        try {
            var formContext = primaryControl;
            var navigationOptions = {
                target: 2,
                height: { value: 80, unit: "%" },
                width: { value: 30, unit: "%" },
                position: 2
            };
            if (formContext.getAttribute("to").getValue() != null || formContext.getAttribute("to").getValue() != undefined) {
                var toId = formContext.getAttribute("to").getValue()[0].id.replace("{", "").replace("}", "").toLowerCase();
              // Open the Modal Dialog Update form in the pop-up window with Matching record ID.
                var pageInput = {
                    pageType: "entityrecord",
                    entityName: "new_customentity",
                    entityId: toId  // Pass record ID (lookupID) which you want to update 
                };
                Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
                    function success() {
                        // Run success code - Add your logic here 
                    },
                    function error() {
                        // Handle errors
                    }
                );
            }
            else {
              // Open the Modal Dialog New Create Form in the pop-up window.
                var pageInput = {
                    pageType: "entityrecord",
                    entityName: "new_customentity"
                };
                Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
                    function success(result) {
                           // Run success code - Add your logic here 
                    },
                    function error() {
                        // Handle errors
                    }
                );
            }
            return true;
        }
        catch (error) {
 
        }
 
    },
};

2. Now navigate to Settings -> Customizations -> Solutions
-> Create a new solution for your ribbon changes 
-> add an email entity (only entity, do not include any dependent file, uncheck and         proceed) to the solution (where you want to place your ribbon button),
-> keep the solution minimum (do not add anything else).

3. Open Ribbon Workbench (you can use XrmToolBox for a better experience): select the solution where you added an email entity and look for an entity left side (verify if the email is as a selected entity there)

4. Navigate to left-hand side inside Toolbox -> Click & hold the button and place it on the form as shown)




5. As highlighted in the image update the ribbon button values:

-> Change the default Id name to proper meaning full name.
-> Change the Label default name to some meaning full name
-> Add Tool Tip title 
-> Add Tool Tip Description
-> Select command (Command you can add after Step No. 6 is completed)


6.  No Go and Add Command (+) 
-> Change the default Id name to proper meaning full name.
-> Add Action: Custom Javascript Action:  in Library file select email                         webresource file by clicking on the search icon,  Add calling Function                     name: NEW.Email.OnclickOfRibbonButton.
-> Click on Add Parameter: Add CRM Parameter and Select Value:                             PrimaryControl 


7. Add Display Rule -> Update appropriate Id name-> Click on Add Display Steps     -> Select FormStateRule -> Set Default: true -> InvertResult: true



8.  Click on: Publish

9. Go to CRM Email Message form look for Ribbon Button and perform click operation. Click On -> Update Button


10.  As the record doesn't exist on the email activity (with passed ID) so a new Modal Dialog pop-up window form gets open.



 
That's all Thank you !!!
Keep Learning...