Understanding Context References in Dynamics 365 Subgrid and Form Customizations

Understanding Context References in Dynamics 365 Subgrid and Form Customizations using Ribbon Workbench

In Dynamics 365, JavaScript customizations offer a powerful way to enhance the user interface and functionality. A common scenario involves custom ribbon buttons that execute JavaScript functions. These functions often require context about the form or subgrid from which they are invoked. This article clarifies how to access and use these contexts effectively.

1. Consistent Context with primaryControl: primaryControl is a key parameter provided to JavaScript functions in Dynamics 365 ribbon commands. It consistently holds the context of the parent form, regardless of its origin - main grid, main form, or subgrid. This uniformity allows developers to reliably manipulate the form's properties and data.

2. Utilizing Parent Form's Context: Accessing the parent form's data via primaryControl follows the standard JavaScript API provided by Dynamics 365:

var formContext = primaryControl; // Directly use primaryControl as formContext
var entityName = formContext.data.entity.getEntityName(); // Get the entity name

3. Accessing Subgrid Details: To interact with subgrid components, selectedControl is used. It provides subgrid-specific information, such as the entity name, which is accessible even when no rows are selected:

function getSubgridEntityName(selectedControl) {
    if (selectedControl && selectedControl.getEntityName) {
      return selectedControl.getEntityName();
    } else {
      console.error('The subgrid control does not provide entity name information.');
      return null;
    }
  }

4. Extracting Parent Form Information from Subgrid: A subgrid control can also provide a pathway to the parent form's context. selectedControl.getParentForm is a method that, if available, allows retrieval of the parent form's entity and form names:

function getParentFormEntityName(selectedControl) {
    var formContext = selectedControl.getParentForm();
    return formContext ? formContext.data.entity.getEntityName() : null;
  }
 
  function getParentFormName(selectedControl) {
    var formContext = selectedControl.getParentForm();
    return formContext ? formContext.ui.formSelector.getCurrentItem().getLabel() : null;
  }

In practice, these functions empower developers to navigate between the contexts of a form and its subgrids, facilitating a deeper integration of custom logic into the CRM's user interface.

No comments:

Post a Comment