Knowledge Center

11 Aug, 2020

How to change view of subgrid conditionally using JavaScript?

Posted on 11 Aug, 2020 by Admin, Posted in Dynamics 365 Dynamics 365 Field Service Dynamics 365 Customer Service Dynamics-365 Sales

How to change view of subgrid conditionally using JavaScript? Blogs

How to change view of subgrid conditionally using JavaScript?


Recently, we got a situation where we must show different fields on sub grid on a condition. We cannot hide or show fields on sub grid, but we can change view of sub grid. In this blog, I have explained how to change view of sub grid on a condition.


Here we are going to use a JavaScript web resource. As an example we took project entity, where project and project template belong to same project entity and we can differentiate between them using “IsTemplate” field. We are going to change view of sub grid on project and project template using this field.

Step by Step

  1. Add JavaScript web resource.
  2. Register function on onLoad event of the project form and pass execution context.
  3. In JavaScript function, get form Context from executionContext
  4. var oFormContext = executionContext.getFormContext();
  5. Get the value of “msdyn_istemplate” field.
  6.  var isTemplate = oFormContext.getAttribute("msdyn_istemplate").getValue();
  7. If the record is a project template, then value of this field will be “true”. it means we will change view if the value is true.
  8. Get the viewSelector using getViewSelector() function.
  9. var viewSelector = oFormContext.getControl(“Subgridname”).getViewSelector(); 
  10. Before that, enable view selector on subgrid.
  11. We are going to set view using setCurrentView(). which requires an object parameter where we need to specify following attribute:
  12. 1. entityType: Number. The object type code for the SavedQuery (1039) or UserQuery (4230) that represents the view the user can select.
    2. id: String. The Id for the view the user can select.
    3. name: String. The name of the view the user can select.
  13. We can get id of the view from address bar when we open that view.
  14. Call setCurrentView() by passing object parameter on using viewSelector.
  15. Final function will be as following:
    function fnChangeViewOfSubgrid(executionContext) {
            "use strict";
            var oFormContext = executionContext.getFormContext();
            if (oFormContext != null) {
                var isTemplate = oFormContext.getAttribute("msdyn_istemplate").getValue();
               if (isTemplate != null && isTemplate == true) {
                    var viewSelector = oFormContext.getControl("Subgridname").getViewSelector();
                    var ProjectTemplateView = {
                        entityType: 1039, // SavedQuery
                        id: "00000000-0000-0000-0000-000d3a5b3f21",
                        name: "View Name"


This is a Required Field


Recent Updates

07 Sep, 2023

Optimizing Storage with SubscriptionTrackingDeletedObject Cleanup

What is SubscriptionTrackingDeletedObject? The "SubscriptionTrackingDeletedObject" table is linked to the "DeletionService," which handles two types of cleanup: organization-wide and record-specific.…

05 Sep, 2023

How to create real-time customer journeys in Dynamics 365 Marketing

Introduction: This blog will show how to create real-time customer journeys in Dynamics 365 Marketing. Customer journeys can be either…

30 Aug, 2023


Introduction: In Dynamics 365 (D365) Customer Service, the 'Customer 360' Component provides a comprehensive view of information and enables users…