Posted in JavaScripts

JavaScript Objects on Subgrid

JavaScript Objects on Subgrid:

These features are available in MS CRM 2016 both on-premises and online.

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

  1. 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()
  1.  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

  1.  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.

  1.  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
  1. getTotalRecordCount();
var recordsCount = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();

Above code snippet will returns the total number of records that match the filter criteria of the view

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s