Search code examples
javascriptdynamics-crmdynamics-crm-onlinedynamics-365

JavaScript Web Resource issue: getGrid() suddenly started failing


I have a few different JavaScript web resources that use the getGrid(), all of which started failing this week after I enabled the 2020 Wave 1 Updates in D365. The error message shows:

"Error occurred :TypeError: Unable to get property 'getGrid' of undefined or null reference"

Here is my code:

function GetTotalResourceCount(executionContext) {
   console.log("function started");
   var execContext = executionContext;
   var formContext = executionContext.getFormContext();
   var resourceyescount = 0;
   try {
      var gridCtx = formContext._gridControl;
       var grid = gridCtx.getGrid();
       var allRows = grid.getRows();
       var duplicatesFound = 0;
       //loop through rows and get the attribute collection
       allRows.forEach(function (row, rowIndex) {
           var thisRow = row.getData().entity;
           var thisRowId = thisRow.getId();
var thisResource = "";
var thisResourceName = "";
var thisResourceID = "";
           console.log("this row id=" + thisRowId);
           var thisAttributeColl = row.getData().entity.attributes;
           thisAttributeColl.forEach(function (thisAttribute, attrIndex) {
               var msg = "";
               if (thisAttribute.getName() == "new_resource") {
                   thisResource = thisAttribute.getValue();
thisResourceID = thisResource[0].id;
thisResourceName = thisResource[0].name;
console.log("this resource name=" + thisResourceName)
               }
           });
           var allRows2 = formContext.getGrid().getRows();
           //loop through rows and get the attribute collection
           allRows2.forEach(function (row, rowIndex) {
               var thatRow = row.getData().entity;
               var thatRowId = thatRow.getId();
               var thatAttributeColl = row.getData().entity.attributes;
var thatResource = "";
var thatResourceName = "";
var thatResourceID = "";
               thatAttributeColl.forEach(function (thatAttribute, attrIndex) {
                   if (thatAttribute.getName() == "new_resource") {
                       thatResource = thatAttribute.getValue();
thatResourceID = thatResource[0].id;
thatResourceName = thatResource[0].name;
                       if (thatResourceID == thisResourceID && thatRowId != thisRowId) {
                           duplicatesFound++;
                           var msg = "Duplicate resource " + thatResource;
console.log("duplicates found= " + duplicatesFound);
                       }
                   }
               });
           });
       });
       if (duplicatesFound > 0) {
           console.log("duplicate found");
Xrm.Page.getAttribute("new_showduplicateerror").setValue(true);
Xrm.Page.getControl("new_showduplicateerror").setVisible(true);
Xrm.Page.getControl("new_showduplicateerror").setNotification("A duplicate resource was found.  Please remove this before saving."); 
       } else {
Xrm.Page.getAttribute("new_showduplicateerror").setValue(false);
Xrm.Page.getControl("new_showduplicateerror").setVisible(false);
Xrm.Page.getControl("new_showduplicateerror").clearNotification(); 
}
   } catch (err) {
       console.log('Error occurred :' + err)
   }
}

Here is a separate web resource that triggers the function:

function TriggerSalesQDResourceCount(executionContext){
var formContext = executionContext.getFormContext();
formContext.getControl("s_qd").addOnLoad(GetTotalResourceCount);
}

Any ideas how I can fix this? Is this a known issue with the new D365 wave 1 update? Thanks!


Solution

  • This is the problem with unsupported (undocumented) code usage, which will break in future updates.

    Unsupported:

    var gridCtx = formContext._gridControl;
    

    You have to switch to these supported methods.

    function doSomething(executionContext) {
       var formContext = executionContext.getFormContext(); // get the form Context
       var gridContext = formContext.getControl("Contacts"); // get the grid context
    
       // Perform operations on the subgrid
    
       var grid = gridContext.getGrid();
    
    }
    

    References:

    Client API grid context

    Grid (Client API reference)