Blog, Development

Synchronizing aggregation fields and subgrids data in MDA forms

In one of my current projects, I received the requirement to synchronize values that are calculated as aggregates from children’s records with the data in form subgrids. In this post, I will provide instructions on how this can be implemented in a model-driven app.

Here is the scenario that I will demonstrate as an example:

  • the account table has a parent-child relationship with the contact table (I will use the standard OOB relationship between account and contact)
  • the account form has a contacts subgrid (Contacts)
  • account has “Related Contacts (Total)”(cr94c_relatedcontactstotal) field of “whole number” type
  • this field is recalculated as the total number of children contacts for the current account
  • recalculation of the field is performed using a synchronous plugin

Generally speaking, the idea of the script is the following – on the “load” event of the grid that fires every time when related record is created, updated, or deleted the function that performs “refresh” of the aggregate columns is executed. It queries aggregation fields from a dataverse using WebApi and “refreshes” the data shown on the form. Here is this function:

Once the function is written and the javascript webresource is uploaded to the Dataverse it’s required to register “entering point” function to the onload event handler of the form. Here is the demo of the mentioned functionality:

Photo by Marvin Meyer on Unsplash

3 Comments

  1. So a calculated column cannot be used for this? I’m not very familiar with the limitations of what these calculated columns can do.

    Also, would this be better/worse than doing the same on the plugin before the record is read?

    1. Hello,
      It’s more about rollup columns and the main limitation of OOB rollup columns is that recalculation of those happening once a day if this recalculation is not forced using the code or other automation.
      But even the recalculation will happen immediately it won’t be reflected on the form. The approach provided will “refresh” values of columns that are calculated as a rollup from children values.
      Also it’s possible to do the plugin but the trick here is that when the record is added/modified/deleted through the subgrid the “refresh” of parent record is not called.
      Andrew

  2. Your step-by-step guide on synchronizing aggregation fields and subgrid data in model-driven apps is incredibly helpful. The scenario you presented with the account and contact tables, along with the “Related Contacts (Total)” field, is a common requirement in many projects, and your solution provides a clear and effective way to address it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.