Thursday, 6 October 2016

Sub Grid client side scripting - CRM 2016

In MS CRM 2016, and Online there are new subgrid related methods added for client side scripting./span>
For subgrid, now we can add event handler for OnLoad. OnLoad event runs every time when subgrid refreshes or when clicking on column heading.
OnLoad Events are addOnLoad and RemoveOnLoad

Xrm.Page.getControl("Opportunities").addOnLoad(OpportunityGridOnloadFunction);
Xrm.Page.getControl("Opportunities").RemoveOnLoad(OpportunityGridOnloadFunction);
To use addOnLoad or RemoveOnLoad you need to get object of grid and then add event handler.
There are some methods to access data from grid.

getRows – gives all rows in the grid
getSelectedRows – gives only selected rows in grid
getTotalRecordCount – gives total number of records in grid.

We can also get Grid Columns, Entityname, Entity Reference, RecordId, Primary Attribute Value
Here is the sample code.

getSubGridRowData = function () {
    //Get Sub Grid object
    var oppSubGrid = Xrm.Page.getControl("Opportunities").getGrid();
    //Get sub grid rows
    var gridRows = oppSubGrid.getRows();
    //  var gridRows = oppSubGrid.getSelectedRows();
    //loop through each row to get values of each column
    gridRows.forEach(function (row, i) {
        var gridColumns = row.getData().getEntity().getAttributes();
        //EntityName
        var entityName = row.getData().getEntity().getEntityName();
        //Entity reference - return type is Lookup object
        var entityReference = row.getData().getEntity().getEntityReference();
        //Record Guid
        var recordId = row.getData().getEntity().getId();
        //Primary Attribute, e.g. for account primary attribute is name, it will return value for name attribute in grid row
        var PrimaryAttributeValue = row.getData().getEntity().getPrimaryAttributeValue();
        //loop through each column in row
        gridColumns.forEach(function (column, j) {
            var atrName = column.getName();
            var atrValue = column.getValue();
        });
    });
}


Hope this helps

--

Happy CRM'ing
Gopinath

No comments:

Post a Comment