Monday, March 2, 2020

Add event on subgrid refresh - Dynamics CRM.

You may need to execute some custom action on your parent form, when you add some records using quick create forms. As an example you may need to disable a field on work order form when you add work order incidents using quick create form.

In this case, you can add a subgrid event listener to your subgrid to perform your logic.
  1.  Create a javascript file as a web resource and add below functions to your javascript file.
  2. function AddSubgridEvents(executionContext) {
      var formContext = executionContext.getFormContext(); 
    
      addSubgridEventListener(formContext);
    } 
    
    function addSubgridEventListener(formContext){
      var gridContext = formContext.getControl("SUBGRID_ID");
      
      //ensure that the subgrid is ready…if not wait and call this function again
      if (gridContext == null){
         setTimeout(function () { addSubgridEventListener(formContext); }, 500);
         return;
      }
      
      //bind the event listener when the subgrid is ready
      gridContext.addOnLoad(subgridEventListener);
    }
    
    function subgridEventListener(gridContext){
        // Gets the form context using subgird context
        var formContext = gridContext.getFormContext();
    
        // Implement your logic here...
    }
  3. Include your javascript file in form libraries.
  4. Add your "AddSubgridEvents" function to Form Onload event handler and pass the execution context as a parameter.
Happy coding 😎