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

Blogs
30 Sep, 2024

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

Introduction Integrating SMS functionality into Dynamics 365 can significantly enhance customer communication by providing a direct and efficient channel for…

READ MORE
Blogs
24 Sep, 2024

White Box Testing - Its Techniques and Types

In this blog, we’ll deal with one of the most popular methods of testing a system or system component thoroughly…

READ MORE
Case Study
06 Sep, 2024

Implementation of Business Central for a Trading Organization

Customer Overview A prominent trading organization sought to enhance its operational efficiency and streamline its business processes by implementing Dynamics…

READ MORE