Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications

|

Subgrid Refresh Events

Post Author: Joe D365 |

Today’s post is a shorty but a goody! It’s a quick fix that will hopefully come in handy for you. Enjoy!

In our experience, it is not unusual when writing custom logic for an entity form that we need to take action when the contents of a subgrid are modified. The Client API provides the ‘addOnLoad’ method of the GridControl object for just such a case. With ‘addOnLoad’ we can assign a listener function to be executed any time the subgrid is loaded (or reloaded by an operation, such as Add). This method operates similarly to a vanilla JavaScript event listener, and it will pass the listener function the execution context as the first parameter.

Makes sense, right? The gotcha comes when trying to call the ‘addOnLoad’ method to apply the binding. If your custom logic is executing before the form has fully rendered, the browser JavaScript engine may throw an ‘Object Undefined’ error. This happens because our code is trying to access the GridControl object before it is available. 

To overcome this, simply wrap the code that applies the binding in a looping construct to “wait” for the GridControl object to become available. Like this (see text below):

var globalFormContext;

function myFormOnload(executionContext) {
globalFormContext = executionContext.getFormContext();

addSubgridEventListener();
}

function addSubgridEventListener(){
var gridContext = globalFormContext.getControl("SUBGRIDNAME");
//ensure that the subgrid is ready…if not wait and call this function again
if (gridContext == null){
setTimeout(function () { addSubgridEventListener(); }, 500);
return;
}
//bind the event listener when the subgrid is ready
gridContext.addOnLoad(subgridEventListener);

}

function subgridEventListener(context){
console.log(‘Take Action on Subgrid Load/Reload Here’);
}

Believe it or not, that’s it! It is such a simple fix – but also a huge help. This will hopefully come in handy for you. Be sure to subscribe to our blog for more tips and tricks.

Happy D365’ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends