Open New form with pre-populated field values in D365 CE(CRM) using JavaScript

In this blog, we will learn to open the ‘new entity’ form or a ‘quick create’ form in D365 CE(CRM).

Use Case:

In my example, I have created a new column(field) of type two option on opportunity. Whenever a user updates its value to YES, a new form of entity “Demo” should open with the pre-populated field values.

Implementation

Syntax:

Xrm.Navigation.openForm(entityFormOptions,formParameters).then(successCallback,errorCallback)

entityFormOptions is an required parameter.

Code to open New Form:

var formCustomizations = {
    openDemoForm: function (executionContext) {
        var formContext = executionContext.getFormContext();
        var createDemo = formContext.getAttribute("vv_createdemo").getValue();
        var opportunityId = formContext.data.entity.getId();
        var opportunityName = formContext.getAttribute("name").getValue();

        if (createDemo === true && createDemo !== null) {
            var entityFormOptions = {};
            entityFormOptions["entityName"] = "vv_demo";

            var formParameters = {};
            formParameters["vv_name"] = "Sample";
            formParameters["vv_description"] = "This is sample record";
            formParameters["vv_age"] = 25;
            formParameters["vv_topic"] = 772500003;
            formParameters["vv_country"] = [772500000, 772500002, 772500005];
            formParameters["vv_interested"] = false;
            formParameters["vv_date"] = new Date();
            formParameters["vv_amount"] = 800.78;
            formParameters["vv_fpn"] = 0.00078;
            formParameters["vv_dn"] = 89.78;

            // Lookup field
            formParameters["vv_author"] = "3793ebd4-ac3f-eb11-a813-000d3a56963a";
            formParameters["vv_authorname"] = "vaishali vyas";
            formParameters["vv_authortype"] = "contact";

            // Lookup field
            formParameters["vv_opportunity"] = opportunityId;
            formParameters["vv_opportunityname"] = opportunityName;
            formParameters["vv_opportunitytype"] = "opportunity";

            // Open the form.
            Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
                function (success) {
                    console.log(success);
                },
                function (error) {
                    console.log(error);
                });
        }
    }
}

NOTE: To open the ‘Quick create’ form, add the below property on entityFormOptions Object.

rest everything will be the same as the code given above.

entityFormOptions["useQuickCreateForm"] = true;

Call the “openDemoForm” function on change of “Create Demo” field.

  • After doing everything successfully, when you will update the ‘create demo’ field value to ‘Yes ‘, a new entity form will open up.

Main Form

Quick Create Form

Leave a Reply

Your email address will not be published. Required fields are marked *