knowledgecenter-breadcrum

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?

Introduction

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.

Solution

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"
                    }
                    viewSelector.setCurrentView(ProjectTemplateView);
                }
            }
        }

Comment

This is a Required Field

Loading

Recent Updates

Thumbnail image
Blogs
13 Feb, 2025

Mastering Project Planning: How to Prevent Delays and Adapt to Any Challenge

Mastering Project Planning: How to Prevent Delays and Adapt to Any Challenge. Introduction Project delays are every project manager’s nightmare.…

READ MORE
Blogs
13 Feb, 2025

Filter List Using FetchXML in Power Pages

Introduction Power Pages provide a flexible way to display data using lists, but sometimes, you may need more advanced filtering than…

READ MORE
SMS_Thumbnail.jpg
Blogs
11 Feb, 2025

SMS Integration with Twilio Number in D365 Customer Service- Part 2

SMS Integration using Twilio Part 2 Introduction In Part 1, we covered below mentioned topics: Get Account SID and Auth…

READ MORE