How to Dynamically Switch Forms in Dynamics 365 Based on Field Values Using JavaScript

 In Dynamics 365, the user interface can be dynamically adjusted to enhance user experience and streamline data entry processes. One way to achieve this is by switching forms based on the value of a specific field. This article provides a detailed guide on how to implement this functionality using JavaScript.

Prerequisites

Before you begin, ensure you have the appropriate permissions to modify forms and write custom JavaScript in Dynamics 365.

Scenario

Consider a scenario where you need to switch the form based on the value selected in a dropdown field. This could be useful in situations where different forms need to be displayed for different types of data within the same entity.

Implementation Steps

  1. Identify the Trigger Field: Choose the field that will trigger the form switch. In our example, this will be an option set or a lookup field.

  2. Map Field Values to Form Names: Create a mapping between the field values and the form names. Each form should correspond to a value in the trigger field.

  3. Write the JavaScript Function: Develop a JavaScript function to handle the logic of switching forms. The function will:

    • Retrieve the current value of the trigger field.
    • Determine the target form based on the mapping.
    • Check if the current form is already the target form to avoid unnecessary reloads.
    • Switch to the target form if necessary.
  4. Register the JavaScript Function: Attach the JavaScript function to the onChange event of the trigger field. Make sure it does not run on form load to prevent infinite loops.

JavaScript Code Example

Here is a sample JavaScript function that demonstrates how to switch forms based on a field's value:


/**
 * Dynamically switches forms based on the value of a specific field.
 * @param {object} executionContext - The execution context from the form event.
 */
function switchForm(executionContext) {
    var formContext = executionContext.getFormContext();
    var triggerField = formContext.getAttribute("your_fieldname"); // Replace with your field name
    if (!triggerField || !triggerField.getValue()) {
        return;
    }

    var fieldValue = triggerField.getValue();
    var formMap = {
        "value1": "FormName1", // Replace value1 and FormName1 with actual values and form names
        "value2": "FormName2"
    };

    var targetFormName = formMap[fieldValue];
    if (!targetFormName) {
        console.warn("No form mapped for this field value: " + fieldValue);
        return;
    }

    var entityName = formContext.data.entity.getEntityName();
    var recordId = formContext.data.entity.getId();
    var currentForm = formContext.ui.formSelector.getCurrentItem();
    var currentFormId = currentForm ? currentForm.getId() : "";

    // Retrieve target form ID from system forms
    var query = `?$select=formid&$filter=type eq 2 and objecttypecode eq '${entityName}' and name eq '${targetFormName}'`;
    Xrm.WebApi.online.retrieveMultipleRecords("systemform", query).then(function (result) {
        if (result.entities.length > 0) {
            var targetFormId = result.entities[0].formid;
            if (currentFormId.toLowerCase() !== targetFormId.toLowerCase()) {
                var parameters = { formid: targetFormId };
                Xrm.Utility.openEntityForm(entityName, recordId, parameters);
            } else {
                console.log("Already on the target form.");
            }
        } else {
            console.warn("No form found with the name: " + targetFormName);
        }
    }, function (error) {
        console.error("Error retrieving forms: " + error.message);
    });
}

Conclusion

Switching forms based on field values in Dynamics 365 can significantly enhance the usability and efficiency of your CRM environment. By following the steps outlined above, you can implement dynamic form switching tailored to your specific business needs.

Further Reading

No comments:

Post a Comment