Auto-Refresh Parent Form After Subgrid Update (e.g., Adding New Rows) in Dynamics 365

 Introduction

In Dynamics 365, ensuring that data presented on forms is up-to-date and consistent is key, especially when interactions involve subgrids and related records. Automating the refresh of a parent form following updates made via a subgrid, such as creating new rows, can significantly enhance user experience and data accuracy.

Custom Button Implementation

A practical approach involves adding a custom button to the subgrid that triggers a Quick Create form. The closure of this form initiates a refresh of the parent form. The JavaScript function linked to this process is structured to be triggered by the ribbon button. The actual code will be implemented here later.

Challenges and Solutions

During the development of this feature, several specific challenges were encountered, particularly related to form navigation and save prompts:

  1. "Leave the page" Prompt:

    • Issue: Initially, trying to refresh the parent form using Xrm.Navigation.openForm resulted in the prompt "Leave this page? You won’t be able to return to this page. To stay here and continue working, select Cancel."
    • Solution: This issue typically arises when attempting to navigate away while the form has unsaved changes. Ensuring that all changes are saved before navigating or using methods that do not trigger navigation can mitigate this prompt.
  2. "Saving in Progress" Prompt:

    • Issue: Adding a save operation within the function triggered an additional prompt: "Saving in Progress. Please wait while saving is completed". This occurred because the function was registered to the onSave event, leading to recursive save triggers.
    • Solution: To resolve this, the save logic needed adjustment. The implementation was refined to ensure that save operations are fully completed before initiating further actions, such as form navigation or refresh, thereby avoiding recursive save triggers.

Effective Solution for Real-Time Updates

Traditional methods for updating a parent form after changes to a subgrid did not meet my needs, often leading to disruptive prompts mentioned in previous section, like "Leave this page?" or "Saving in Progress." These challenges hindered the workflow and failed to ensure timely updates.

The solution that worked best was implementing a custom button using Xrm.Navigation.openForm. This approach successfully avoided the earlier issues, allowing for seamless integration of new data entries and immediate reflection of these updates on the parent form. The callback feature of Xrm.Navigation.openForm was crucial, enabling precise control over the form refresh process after closing the Quick Create form, thereby ensuring consistent data and improving user interaction.

Next Steps

I will detail the specific code implementation and provide visuals from the Ribbon Workbench setup that demonstrate how the custom button was configured to initiate the JavaScript function effectively. This guidance will help replicate similar functionality in Dynamics 365 environments.


Your can reuse the following JavaScript Function directly.


/**
 * Opens a Quick Create form for creating a new record, linked to a parent entity. This function can be triggered by a ribbon button.
 *
 * Parameters:
 * - primaryControl: The form context, typically passed as 'PrimaryControl'.
 * - selectedControl: The subgrid control.
 * - parameters (Optional): A stringified JSON object with static values for pre-populating fields.
 *
 * Example of static parameters:
 * - '{"firstname": "John", "lastname": "Doe"}'
 * This JSON string sets the 'firstname' to "John" and 'lastname' to "Doe" in the Quick Create form.
 */

function openQuickCreateForm(primaryControl, selectedControl, parameters = {})
{
    // Get the entity name from selectedControl, it's a subgrid
    var subgridEntityName = selectedControl.getEntityName();
    var parentFormEntityName = primaryControl.data.entity.getEntityName();
    var parentRecordId = primaryControl.data.entity.getId();

    // Construct the parent entity reference for the 'createFromEntity' option
    var parentEntityReference =
    {
        entityType: parentFormEntityName,
        id: parentRecordId
    };

    var entityFormOptions =
    {
        entityName: subgridEntityName,
        useQuickCreateForm: true,
        createFromEntity: parentEntityReference // This sets the context for the new record
    };

    // Open a Quick Create form with the options and parameters
    Xrm.Navigation.openForm(entityFormOptions, parameters).then(
        function () {
            // Refresh parent form after the Quick Create form is closed
            primaryControl.data.refresh();
        },
        function (error) {
            console.error("Error opening Quick Create form:", error);
        }
    );
}


Ribbon Workbench:



Understanding Xrm.Navigation.openForm

Xrm.Navigation.openForm is crucial for opening forms within Dynamics 365. It is designed to handle form openings dynamically and includes a callback function that triggers once the form is closed. This feature is particularly useful for tasks that need to be executed after the form is no longer active, such as refreshing data on a parent form to reflect recent changes.

Conclusion

Implementing an automated process to refresh a parent form following subgrid updates not only streamlines workflows but also ensures that users are working with the most current information. This reduces the need for manual refreshes and enhances overall data integrity within Dynamics 365 environments. By addressing the specific challenges associated with form navigation and save prompts, developers can create more reliable and user-friendly solutions.

No comments:

Post a Comment