Answered by:
getGrid().getRows() always returns 0 CRM 2016 on-premise

Question
-
Hi,
Anyone knows if there's a problem with the subgrid implementation in crm 2016? This is the code I'm using but I never get any records from the grid even though a reference to the grid is returned.
SetProductGridOnLoad: function () { var productGridControl = Xrm.Page.getControl("subgrid_products"); productGridControl.addOnLoad(FBCrm.Lead.SetProductGridTimer); }, SetProductGridTimer: function() { setTimeout(FBCrm.Lead.CalculateLeadProductsTotal, 2000); }, /* * Re-Calculates the total when a new product is added to the * Lead products grid */ CalculateLeadProductsTotal: function () { var productGrid = Xrm.Page.getControl("subgrid_products").getGrid(); var productRows = productGrid.getRows(); // this always returns 0 var total = 0; productRows.forEach(function (attribute, index) { var rowEntity = attribute.getData().getEntity(); var listPrice = rowEntity.getAttributes().get("price"); total += listPrice; }); Xrm.Page.getAttribute("cust_totalprice").setValue(total); Xrm.Page.getAttribute("cust_totalprice").setSubmitMode("always"); },
Thanks
Tony
Tony
Friday, April 1, 2016 2:28 PM
Answers
-
Hi Chris,
Isn't
var rows = Xrm.Page.getControl("Contacts").getGrid().getRows();
the same as
var productGrid = Xrm.Page.getControl("subgrid_products").getGrid(); var productRows = productGrid.getRows(); // this always returns 0
I used that example to write this code but it doesn't work as detailed in that code sample.
The problem is not the getRows(). getRows() always returns an object the problem is that forEach never returns an object from the collection.
A second problem I found is that productRows.getLength() always returns 0 when the subgrid OnLoad first calls the function.
This is how I finally managed to get this working:
CalculateLeadProductsTotal: function () { var productGrid = Xrm.Page.getControl("subgrid_products").getGrid(); var productRows = productGrid.getRows(); var rowCount = productRows.getLength(); var total = 0; if (rowCount == 0) { setTimeout(FBCrm.Lead.CalculateLeadProductsTotal, 2000); return; } for (var i = 0; i < rowCount; i++) { var rowEntity = productRows.get(i).getData().getEntity(); var listPrice = rowEntity.getAttributes().get("price").getValue(); total += Number(listPrice.replace(/[^0-9\.]+/g, "")); } /* This is not working as documented in the sdk documentation productRows.forEach(function (attribute, index) { var rowEntity = attribute.getData().getEntity(); var listPrice = rowEntity.getAttributes().get("price"); total += Number(listPrice.replace(/[^0-9\.]+/g, "")); }); */ Xrm.Page.getAttribute("cust_totalprice").setValue(total); Xrm.Page.getAttribute("cust_totalprice").setSubmitMode("always"); },
TonyTony
- Marked as answer by Tony Amaral Friday, April 1, 2016 5:34 PM
Friday, April 1, 2016 5:33 PM
All replies
-
Have you tried adding .getRows() right after the getGrid()? This example I pulled shows it done that way.
var allGridRowData = []; var rows = Xrm.Page.getControl("Contacts").getGrid().getRows(); rows.forEach(function (row, i) { allGridRowData.push(row.getData()); });
Friday, April 1, 2016 4:53 PM -
Hi Chris,
Isn't
var rows = Xrm.Page.getControl("Contacts").getGrid().getRows();
the same as
var productGrid = Xrm.Page.getControl("subgrid_products").getGrid(); var productRows = productGrid.getRows(); // this always returns 0
I used that example to write this code but it doesn't work as detailed in that code sample.
The problem is not the getRows(). getRows() always returns an object the problem is that forEach never returns an object from the collection.
A second problem I found is that productRows.getLength() always returns 0 when the subgrid OnLoad first calls the function.
This is how I finally managed to get this working:
CalculateLeadProductsTotal: function () { var productGrid = Xrm.Page.getControl("subgrid_products").getGrid(); var productRows = productGrid.getRows(); var rowCount = productRows.getLength(); var total = 0; if (rowCount == 0) { setTimeout(FBCrm.Lead.CalculateLeadProductsTotal, 2000); return; } for (var i = 0; i < rowCount; i++) { var rowEntity = productRows.get(i).getData().getEntity(); var listPrice = rowEntity.getAttributes().get("price").getValue(); total += Number(listPrice.replace(/[^0-9\.]+/g, "")); } /* This is not working as documented in the sdk documentation productRows.forEach(function (attribute, index) { var rowEntity = attribute.getData().getEntity(); var listPrice = rowEntity.getAttributes().get("price"); total += Number(listPrice.replace(/[^0-9\.]+/g, "")); }); */ Xrm.Page.getAttribute("cust_totalprice").setValue(total); Xrm.Page.getAttribute("cust_totalprice").setSubmitMode("always"); },
TonyTony
- Marked as answer by Tony Amaral Friday, April 1, 2016 5:34 PM
Friday, April 1, 2016 5:33 PM -
Glad you got it working. Thanks!Friday, April 1, 2016 5:36 PM