Opening Specified Dynamics 365 Main Forms in a Pop-up Window with Pre-populated Fields Using JavaScript

 

Opening Specified Dynamics 365 Main Forms in a Pop-up Window with Pre-populated Fields Using JavaScript

Introduction

Navigating to a specific Dynamics 365 main form in a pop-up window with pre-populated fields can significantly improve the user experience by providing a streamlined and focused interaction. This article outlines a JavaScript approach that enables opening specified main forms in a new window and pre-populating fields, with options for customization.

Code Explanation

The JavaScript function openSpecificMainForm opens a Dynamics 365 form in a new browser window, allowing for specification of the form and pre-population of fields.

Function openSpecificMainForm

/**
 * Opens a specific Dynamics 365 form in a new window, with the option to specify the form and pre-populate fields.
 *
 * @param {string} entityLogicalName - The logical name of the entity.
 * @param {string} [formId] - Optional. The GUID of the form to be opened.
 * @param {Object|string} [fieldValues] - Optional. An object or JSON string representing field values to pre-populate.
 */
function openSpecificMainForm(entityLogicalName, formId, fieldValues)
{
    var extraqs = formId ? `formid=${formId}` : '';
   
    // Check if fieldValues is a string and not empty
    var fieldValuesObj = {};
    if (typeof fieldValues === 'string' && fieldValues.trim() !== '') {
        fieldValuesObj = JSON.parse(fieldValues);
    } else if (typeof fieldValues === 'object' && fieldValues !== null) {
        fieldValuesObj = fieldValues;
    }

    for (var fieldName in fieldValuesObj) {
        if (fieldValuesObj.hasOwnProperty(fieldName)) {
            extraqs += `${extraqs ? '&' : ''}${fieldName}=${fieldValuesObj[fieldName]}`;
        }
    }

    extraqs = encodeURIComponent(extraqs);

    var createFormUrl = Xrm.Utility.getGlobalContext().getClientUrl() +
        `/main.aspx?etn=${entityLogicalName}&pagetype=entityrecord&extraqs=${extraqs}`;

    window.open(createFormUrl, "_blank", "height=600,width=800,scrollbars=1,resizable=1");
}


Sample Usage


Used on Ribbon Workbench for custom button



Sample result:

Conclusion

This versatile JavaScript function caters to various scenarios in Dynamics 365, from opening a default form to navigating to a specific form with fields pre-populated. It's particularly useful for custom ribbon buttons, script-triggered navigation, or other scenarios requiring enhanced user interaction.

No comments:

Post a Comment