Tuesday, October 4, 2016

Sub Grid client side script methods


In MS CRM 2016, and Online there are new subgrid related methods added for client side scripting
Events
            For subgrid now you 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
To use addOnLoad or RemoveOnLoad you need to get object of grid and then add event handler.
Xrm.Page.getControl("Contacts").addOnLoad(myContactsGridOnloadFunction);
Xrm.Page.getControl("Contacts").RemoveOnLoad(myContactsGridOnloadFunction);

Access Data from grid
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.

getRows data

getSubGridRowData: function () {
        //Get Sub Grid object
        var carsubGrid = Xrm.Page.getControl("Cars").getGrid();

        //Get sub grid rows
        var gridRows = carsubGrid.getRows();

        //loop through each row to get values of each column
        gridRows.forEach(function (row, i) {
            var gridColumns = row.getData().getEntity().getAttributes();
            //loop through each column in row
            gridColumns.forEach(function (column, j) {
                var atrName = column.getName();
                var atrValue = column.getValue();
            });
        });

    }

getSelectedRows data

getSelectedSubGridRowData: function () {      
        //Get Sub Grid object
        var carsubGrid = Xrm.Page.getControl("Cars").getGrid();

        //Get sub grid rows
        var gridRows = carsubGrid. getSelectedRows();

        //loop through each row to get values of each column
        gridRows.forEach(function (row, i) {
            var gridColumns = row.getData().getEntity().getAttributes();
            //loop through each column in row
            gridColumns.forEach(function (column, j) {
                var atrName = column.getName();
                var atrValue = column.getValue();
            });
        });

    }

GetTotalRecordCount in grid

getSubGridRecordCount:function()
    {
        var carsubGrid = Xrm.Page.getControl("Cars").getGrid();
        var TotalRecordCount = carsubGrid.getTotalRecordCount();
    },

GridEntity details
To get grid entity details you can use
1.      getEntityName – gives logical name for the record in the sub grid row
2.      getEntityReference - Set the firstEntityType variable to an entity reference for the first row in the Contacts subgrid.
3.      getId – returns GUID for the record in subgrid row
4.      getPrimaryAttributeValue - Returns the primary attribute value for the record in the row.

getGridEntityDetails: function () {
        //Get Sub Grid object
        var carsubGrid = Xrm.Page.getControl("Cars").getGrid();

        //Get sub grid rows
        var gridRows = carsubGrid.getRows();
        gridRows.forEach(function (row, i) {

            //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();
        });

    },

Subgrid view methods
If the subgrid control is not configured to display the view selector, calling the ViewSelector methods will throw an error.
1.      getCurrentView – gives current view details , it is lookup object, contains Id and name of saved query entity or user query entity.
2.      setCurrentView – set current view to subgrid, it is lookup object of SavedQuery entity or user query entity.
3.      isVisible - Use this method to determine whether the view selector is visible.

gridViewSelectorMethods: function () {
        var carsubGrid = Xrm.Page.getControl("Cars").getViewSelector();

        //gives current view
        var currentView = carsubGrid.getCurrentView();

        //set current view
        var myCurrentCars = {
            entityType: 1039, // SavedQuery
            id: "{3A282DA1-5D90-E011-95AE-00155D9CFA02}",
            name: "My Cars"
        }
        //Set the view using myCurrentCars
        Xrm.Page.getControl("Cars").getViewSelector().setCurrentView(myCurrentCars);

        // is Visible
        var viewSelectorIsVisible = Xrm.Page.getControl("Cars").getViewSelector().isVisible();
    },


More details about grid methods and functions


7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Which methods are particularly added in 2016 that were not the part of CRM 2015?

    ReplyDelete
  3. Hi Ali,
    In CRM 2015 Update 1 all of the above methods added. before Update 1 there was only Refresh method for subgrid.

    Check this link

    https://msdn.microsoft.com/en-us/library/dn932126(v=crm.7).aspx

    ReplyDelete
  4. Hi Ali,
    In CRM 2015 Update 1 all of the above methods added. before Update 1 there was only Refresh method for subgrid.

    Check this link

    https://msdn.microsoft.com/en-us/library/dn932126(v=crm.7).aspx

    ReplyDelete
  5. hi mahadeo i am trying to get record count but it is showing 0 though there r records pls rply regarding this

    ReplyDelete
  6. i observed when enabling this Js the + of subgrid is not appearing after disabling + is showing

    ReplyDelete
  7. Webcrs award winning, beneficial CRM for Tour Operators software provides everything for a small & large scale business operators to run their back office & sales operation in one easy powerful system! With webcrs travel CRM, tour operators can get entire 360degree customer view.

    ReplyDelete