Custom Button on Subgrid to Create New Related Records with Pre-Populated Parameters in Dynamics 365

 


Custom Button on Subgrid to Create New Related Records with Pre-Populated Parameters in Dynamics 365

In the world of CRM systems, especially in platforms like Microsoft Dynamics 365, the ability to streamline and automate processes is key to efficiency. One useful customization is adding a custom button to a subgrid that creates a new related record, with certain fields pre-populated based on the context of the parent record. This article walks through how to implement such a feature using a JavaScript function and Ribbon Workbench.

Scenario Overview

Imagine you have a subgrid of Vendor records in Dynamics 365, and you want to create a related Contact record with specific fields pre-populated. This scenario is particularly helpful in maintaining data integrity and saving time in data entry. To achieve this, we'll use a custom JavaScript function that is triggered by a button in the Ribbon Workbench interface.

Step-by-Step Implementation

  1. Ribbon Workbench Setup:

    • First, you'll need to create a custom button in Ribbon Workbench. This button will be configured to call a JavaScript function when clicked.


    •  
  2. JavaScript Function:

    • The core of this customization is a JavaScript function that opens a new entity form with specific fields pre-populated.
    • Below is the code for the function with comments explaining each part:
javascript
/**
 * Opens a related entity form with pre-populated field values.
 *
 * @param {string} relatedEntityName - The logical name of the related entity.
 * @param {object} primaryControl - The primary form context.
 * @param {object} parameters - Key-value pairs to pre-populate fields in the form.
 *
 * sample:
* // Pre-populate the 'firstname' and 'lastname' fields in the related entity form
 *   parameters = {
 *     "firstname": "John",
 *     "lastname": "Doe"
 * };
 */
function OpenRelatedEntityFormWithPrePopulation(relatedEntityName, primaryControl, parametersString)
{
    debugger;

    console.log("Parameters String:", parametersString);

    var parameters = {};
    try {
        // Convert the parameters string to an object
        parameters = JSON.parse(parametersString);
    } catch (e) {
        console.error("Error parsing parameters:", e);
        return;
    }

    var entityFormOptions = {
        entityName: relatedEntityName,
        createFromEntity: {
            entityType: primaryControl.data.entity.getEntityName(),
            id: primaryControl.data.entity.getId()
        },
        openInNewWindow: true
    };

    Xrm.Navigation.openForm(entityFormOptions, parameters).then(
        function (success) {
            console.log("Related entity form opened successfully.");
        },
        function (error) {
            console.log("Error opening related entity form:", error);
        }
    );
}

  1. Function Explanation:

    • OpenRelatedEntityFormWithPrePopulation: This function takes three parameters – the name of the related entity (relatedEntityName), the context of the parent record (primaryControl), and a string containing JSON-formatted key-value pairs (parametersString).
    • The function parses the parametersString to an object and uses it in the Xrm.Navigation.openForm method to open the new related entity form with the specified fields pre-populated.
  2. Adding the Button in Ribbon Workbench:

    • In Ribbon Workbench, create a new button on the desired subgrid.
    • Set the button's command to call OpenRelatedEntityFormWithPrePopulation, passing the appropriate parameters.
  3. Testing and Deployment:

    • Once set up, test the button to ensure it opens the correct form with the intended fields pre-populated.
    • After successful testing, deploy the changes to your Dynamics 365 environment.

Conclusion

This customization enhances user experience by reducing manual data entry and ensuring consistency in data creation. The integration of JavaScript with Ribbon Workbench offers a powerful way to tailor Dynamics 365 to your specific business processes.

No comments:

Post a Comment