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

Loading

Recent Updates

Thumbnail_top 10 benefits of
Blogs
02 Dec, 2024

Top 10 Benefits of Microsoft Dynamics 365

This blog lists the advantages of Microsoft Dynamics 365, focusing on how it can improve your business operations.    …

READ MORE
Thumbnail_Microsoft365 Vs D365
Blogs
12 Nov, 2024

Microsoft 365 vs Dynamics 365

This blog will help you understand the key difference between Microsoft 365 and Dynamics 365, so you can make an…

READ MORE
Thumbnail_What is Microsoft Dynamics 365
Blogs
05 Nov, 2024

What is Microsoft Dynamics 365?

This blog explores Microsoft Dynamics 365, a cloud solution for handling different business activities smoothly. The rising trends of competition…

READ MORE