Monday, October 5, 2015

JavaScript Objects on Subgrid

These features are available in MS CRM 2015 online with update 1 and available in MS CRM 2016 both on-premises and online.

With latest version of MS CRM 2015 we are able to perform certain JavaScript functions against SubGrid. Below are list of useful objects which we can use in most of the requirements.

1.  Get EntityName of SubGrid:

Xrm.Page.getControl("States").getEntityName();

This function will returns the logical name of entity that we are using in SubGrid

2.  getGrid

Next property we can use for SubGrid is getGrid.

var subGridObject=Xrm.Page.getControl("States").getGrid();

This will returns Mscrm.XrmControlGridWrapper as object. This return object has important properties like

·         getRows()
·         getSelectedRows()
·         getTotalRecordCount()
·         getFilter()

3.  getViewSelector

 var subGridViewSelector=Xrm.Page.getControl("States").getViewSelector();

This JavaScript will return XrmViewSelectorWrapper object which contains the properties like

·         getCurrentView()
·         setCurrentView()
·         isVisible().

To retrieve the name of current view we can use below code snippet
Xrm.Page.getControl("States").getViewSelector().getCurrentView().name

4.  getRows()

var allRows = Xrm.Page.getControl("States").getGrid().getRows();
Returns collection of GridRow of SubGrid. We can access all SubGrid rows by iterating
the collection.
5.  getSelectedRows()

var selectedRows = Xrm.Page.getControl("States").getGrid().getSelectedRows();
    selectedRows.forEach(function (selectedRow, i) {
        var s = selectedRow.getData().getEntity().getEntityReference();
         var selectedRowGuid=s.id;
        var selectedRowName=s.name;
        var selectedRowEntityName=s.entityType;

    });

The above code snippets returns collection of selected rows and by iterating the collection we can retrieve the properties like 

·         id - Here id is nothing but Guid of record
·         name - Here name will return the value in name fields
·         entityType - This will return the logical name entity which is displaying as SubGrid

5.  getTotalRecordCount();

var recordsCount = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();



2 comments:

  1. getSelectedRows is not working as expected.
    Trying to debug in IE debugger tools, it doesn't enter the for loop itself.

    ReplyDelete
    Replies
    1. Hi Ashish, which version of CRM you are using, this will works only for MS CRM 2015 online with update 1 or MS CRM 2016 (Online and onpremises). It won't work for MS CRM 2015 on-premises and below versions.

      Delete