Saturday 23 November 2013

Read SubGrid Records MS CRM 2013 using JavaScript !!!

Many times we have requirements to read sub grid records and do subtotal or some others stuff with grid data. Here is same code to retrieve entire rows and columns from SubGrid.

function RetrieveSubGridRecords() {
    if (document.getElementById("SubGridName")) {
        var grid = document.getElementById("SubGridName").control;
        for (var rowNo = 0; rowNo < grid.GetRecordsFromInnerGrid().length; rowNo++)
            for (var cellNo = 0; cellNo < grid.GetRecordsFromInnerGrid()[rowNo][3].cells.length; cellNo++)
                alert(grid.GetRecordsFromInnerGrid()[rowNo][3].cells[cellNo].outerText);
    }
    else {
        setTimeout("RetrieveSubGridRecords();", 2500);
    }

}


Hope it helps. J

6 comments:

  1. Vikram, nice post, How can we find the subgridname on the Account form??
    I tried bunch of option but no luck.

    ReplyDelete
  2. I have a requirement that is easier, I just need to check if there are ANY records in the grid and if it is empty (no associated records) I need to display and error message. How would that look? Thanks.

    ReplyDelete
  3. The code is unsupported as it uses GetElementById...

    ReplyDelete
  4. var x = window.parent.document.getElementById("SubGridName").control;

    this returns me "undefined" when getting to use this code on 2015 online.

    anyone have an work around? I want to code to work on 2015 on-premise so I cann't use the resent subgird methods given to 2015 online version.

    ReplyDelete
  5. this will work on dynamics crm online

    var ctrl = Xrm.Page.getControl(SubgridName);
    var selectedRows = ctrl.getGrid().getSelectedRows();
    var selectedRow = selectedRows.getAll()[0];
    var recordId = selectedRow.getData().getEntity().getEntityReference().id;
    var attributes = selectedRow.getData().getEntity().getAttributes().getAll();

    attributes.forEach(function (attribute) {
    console.log(attribute.getKey() + "|" + attribute.getValue());
    });

    ReplyDelete
  6. is it work with crm 2016?

    ReplyDelete