web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / MS CRM Customization / Sub Grid client side script...

Sub Grid client side script methods

Mahadeo Matre Profile Picture Mahadeo Matre 17,021

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 ViewSelectormethods 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


This was originally posted here.

Comments

*This post is locked for comments