Refresh parent entity when creating a quick record from a subgrid

I recently had a requirement from my client which was :

  • update some fields on the parent record when creating/upating/deleting a record from a subgrid on this record.

Nothing is better than a picture to understand the use case :
As soon as we create/delete/update a record from the subgrid, we update the revenue field from the opportunity.

Unfortunately, it's not possible to do this via a standard/ootb feature of the CRM (or I didn't find out !).

My process is then :

  • Add an event on the grid to be refreshed when a record is added/modified/deleted (which will be used to refresh the data of the parent record)
  • Make sure that the "+" open a quick create form.
  • Write a plugin to update the opportunity revenue fields when an Expected Revenue is modified (doing the sum of all expected revenue and pushing the values into the correct fields)

To add an event to refresh the grid, it's simple :

First step : attaching the event on the grid
var grid = Xrm.Page.getControl(gridName);  
// if the subgrid still not available we try again after 1 second
if (grid == null) {  
    setTimeout(function () { addEventToGridRefreshFunction(gridName, functionToCall); }, 1000);
    return;
}

// add the function to the onRefresh event
grid.addOnLoad(functionToCall);  

Sometimes, the sub-grids take more time than other controls to load on the form, that's why I've added the setTimeout function to make sure that if you javascript code is reached before the control is available, we wait for 1 second and then we try again to attach the event to the control.

Add as soon as the control is available, we add to it a "addOnLoad" function.

Second step : do something when the event is triggered

Now let's see how to use the "functionToCall".

In the same idea as above, we will use the setTimeout function to make sure that the control is available to do something :

try {  
    //setting timeout beacuse subgid take some time to load after the form is loaded
    setTimeout(function () {
        //validating to check if the sub grid is present on the form
        if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl(expectedRevenueGridName) != null && Xrm.Page.getControl(expectedRevenueGridName) != undefined) {
            Xrm.Page.data.refresh();
        }
    }, 1000);
} catch (e) {
    console.log("Error: " + (e.message || e.description));
}

As you see, when my gridControl is available, I make sure that I refresh the data of my opportunity record.

Thrid step : write your plugin to update the Opportunity revenues fields on the Pre/Post action based on your requirements
Conclusion

So if I sum up here :

  1. Add an event to your subgrid to know when it's refreshed (you have to know that as soon as you do an action from the quick create form of a subgrid, the grid is refreshed automatically)
  2. Write your plugin or whatever you need to process the data in the background
  3. Refresh the parent record once the record from the subgrid got an action.

With this process, you will cover the create/delete directly from the subgrid and the edit will open the record in the main form so when you will get back to the parent record, the data will be refreshed anyway.

Hope it will help you.
Happy CRM'ing,

Clément

Clement

Dynamics 365 CRM & Power Platform addict.