Customizing Dynamics 365: Opening Specific Forms with Custom Buttons

 Customizing Dynamics 365: Opening Specific Forms with Custom Buttons

Introduction Customizing user experience in Dynamics 365 can significantly improve efficiency and user satisfaction. One way to achieve this is by adding custom buttons to open specific main and Quick Create forms. This article introduces how to implement such customizations using JavaScript.

Opening a Specific Main Form To open a specified main form, you can use the openForm method. This method allows you to specify the form's GUID to ensure that the correct form is opened. Here's an example:

javascript
function openSpecificMainForm() {
    var entityFormOptions = {
        entityName: "account",
        formId: "your-form-id-here"
    };
    Xrm.Navigation.openForm(entityFormOptions).then(
        function (success) { console.log("Form opened successfully."); },
        function (error) { console.log("Error opening form:", error); }
    );
} 

Replace "account" with the entity logical name and "your-form-id-here" with the form's GUID.

Pre-Populating Fields in Main Forms You can also pre-populate fields when opening a main form. This is achieved by passing parameters with the openForm method. Here's an example:

javascript

function openMainFormWithParameters() {
    var entityFormOptions = { entityName: "contact", formId: "your-form-id-here" };
    var parameters = {};
   
    parameters["firstname"] = "John"; // Pre-populating the 'firstname' field
    parameters["lastname"] = "Doe"; // Pre-populating the 'lastname' field
    parameters["your_optionset_fieldname"] = value; // Replace with the field name and the option set value (integer)
    parameters["your_lookup_fieldname"] = [{
        id: "record_guid", // GUID of the lookup record
        entityType: "entity_logical_name", // Logical name of the entity
        name: "record_display_name" // Display name of the record
    }];



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

This script opens a specific main form for a contact entity and pre-populates the 'firstname' and 'lastname' fields.

Opening a Quick Create Form

For Quick Create forms, the openQuickCreate method opens the default Quick Create form for the specified entity. Unlike main forms, you cannot specify which Quick Create form to open. Sample code:

javascript
function openQuickCreateForm() {
    Xrm.Utility.openQuickCreate("account").then(
        function (result) { console.log("Quick Create form opened successfully."); },
        function (error) { console.log("Error:", error.message); }
    );
}


Pre-Populating Fields in Quick Create Forms In addition to opening default Quick Create form, you can also pre-populate fields when opening a Quick Create form. Here are two scenarios:

  1. Pre-Populating Based on Current Record:

    javascript
    var parameters = { name: "Child account of " + Xrm.Page.getAttribute("name").getValue() };
    Xrm.Utility.openQuickCreate("account", null, parameters).then(
        // Callback functions
    );

    This opens a Quick Create form for an account, setting the 'name' field based on the current record's name.

  2. Pre-Populating with Static Values:

    javascript
    function openQuickCreateWithParameters() {
        var entityName = "contact"; // Replace with your entity logical name
        var parameters = {};
       
        parameters["firstname"] = "John"; // Pre-populating the 'firstname' field
        parameters["lastname"] = "Doe"; // Pre-populating the 'lastname' field
        parameters["your_optionset_fieldname"] = value; // Replace with the field name and the option set value (integer)
        parameters["your_lookup_fieldname"] = [{
            id: "record_guid", // GUID of the lookup record
            entityType: "entity_logical_name", // Logical name of the entity
            name: "record_display_name" // Display name of the record
        }];

        Xrm.Utility.openQuickCreate(entityName, null, parameters).then(
            function (result) {
                console.log("Quick Create form opened successfully.");
            },
            function (error) {
                console.log("Error:", error.message);
            }
        );
    }

    This opens a Quick Create form for a contact and pre-populates the 'firstname' and 'lastname' fields.

Conclusion Integrating these custom buttons into your Dynamics 365 environment can streamline workflows and enhance user interactions. Remember to test these

No comments:

Post a Comment