PowerObjects Blog 

for Microsoft Business Applications

|

Subgrid Refresh Events

|

Subgrid Refresh Events

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) {
  var 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 Kieran Patel

Leave a Reply

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

PowerObjects Recommends