Creating a Custom Ribbon Button in Dynamics 365: Conditional Display Based on Field Value and User's Security Role with Specified Actions

 Creating a Custom Button on a Main Form Using Ribbon Workbench in Dynamics 365

In Dynamics 365, tailoring the user interface according to specific business processes enhances user experience. This guide demonstrates how to create a custom button on an entity's main form. This button has conditions for visibility and will execute a specific JavaScript action when clicked.

Requirements:

  1. The button should only be visible when a specific field (e.g., "Status Field") is set to certain values.
  2. Only users with specific roles should see the button.
  3. When clicked, the button should modify a particular field's value based on the provided JavaScript.

Prerequisites:

  • Access to Dynamics 365 with customization rights.
  • XrmToolBox / Ribbon Workbench installed.

Step-by-Step Guide:

1. JavaScript Web Resources:

a. Button Action Function:

Prepare a JavaScript function to be executed when the button is clicked.

javascript
SetFieldAction = function (primaryControl, fieldName, val, confirmation) { var formContext = primaryControl; if (val != "null") { var confirmResponse = true; if (confirmation) { confirmResponse = confirm(confirmation); } if (confirmResponse) { formContext.getAttribute(fieldName).setValue(parseInt(val)); formContext.data.entity.save(); } } else { formContext.getAttribute(fieldName).setValue(null); } }

b. User Role Check Function:

Here's a JavaScript function to check if the user has one of the specified roles:

javascript
async function checkUserRole(primaryControl, targetRoleName) { const fetchXml = ` <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"> <entity name="role"> <attribute name="name" /> <order attribute="name" descending="false" /> <link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true"> <link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ab"> <filter type="and"> <condition attribute="systemuserid" operator="eq-userid" /> </filter> </link-entity> </link-entity> </entity> </fetch>`; try { const result = await Xrm.WebApi.retrieveMultipleRecords("role", "?fetchXml=" + encodeURIComponent(fetchXml)); const userRoles = result.entities.map(role => role.name); return userRoles.includes(targetRoleName); } catch (error) { console.error(error); throw error; } }

Upload both scripts as web resources in your D365 instance.

2. Open Ribbon Workbench:

  • Select a solution containing your target entity.
  • Open the solution in Ribbon Workbench.

3. Add Custom Command:

  • In the "Commands" tab, create a new command.
  • For the command action, select "JavaScript Function" and provide:
    • Crm Param: Primary Control
    • Str Param: Desired field's logical name
    • Int Param: Value to set in the field

4. Configure Display and Enable Rules:

For the first requirement of display based on specified field's values, it can be easily realized by using Display Rules.

However, the second requirement of display based on current user's security role which needs to use Custom Rule to call a JavaScript function to determine the display result is true or false.

a. Display Rules:

  • For the command's display rule, set an "OR" rule targeting the values for the "Status Field".
  • Ensure "Default" and "InvertResult" are set to "False".


b. Enable Rules:

  • Use a "Custom Rule" with the checkUserRole JavaScript function to verify user roles.
  • The button will be enabled if the user has any of the specified roles and if the display rule conditions are met.

(Note: Display rules and enable rules operate in conjunction. Both conditions must be satisfied for the button to be visible.)

5. Create and Link the Button:

  • In Ribbon Workbench, drag a new button onto the form ribbon of the target entity.
  • Link this button to the command crafted in step 3.


6. Publish Changes:

After configuration, publish your changes via Ribbon Workbench.


By adhering to this guide, you'll be able to craft a custom button on any entity's main form in Dynamics 365, ensuring users encounter a tailored and efficient experience.

No comments:

Post a Comment