web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Set default view of Su...
Power Apps
Unanswered

Set default view of Subgrid dynamically in Model-Driven Apps

(0) ShareShare
ReportReport
Posted on by 284

Hello guys,

I have a column (Choice-Column type ) rh_locations with Location A/B/C

Then I have a lookup to a table Phonebook.

My Plan is, based on the selected Location the default view of the subgrid should change to other views I already made (filtered by Location)

I tried the following code in a JSON and made an EventHandler OnLoad of the Form and OnChange on the Choice Column. 

The Code doesn't throw an error but it won't work.

 

var OnBoarding = window.OnBoarding || {};

(
 function() {
 this.setDefaultView = function(executionContext){
 var officeLocation = Xrm.Page.getAttribute("rh_standort").getValue();
 var lookupField = Xrm.Page.getControl("rh_geschaftsnummer");
 // Check the selected office location and set the appropriate view

 if (officeLocation === "Naila") {
 lookupField.setDefaultView("bf3591dd-7eb6-ed11-83fe-0022489ca5c5");
 } else {
 lookupField.setDefaultView("488fdd3f-7fb6-ed11-83fe-0022489ca5c5");
 }
 }
 }
).call(OnBoarding)

Can you help me?

Categories:
I have the same question (0)
  • EricRegnier Profile Picture
    8,720 Most Valuable Professional on at

    Hi @RiccoHipp1903,

    Not sure if you manage to resolve this, but based on the code, you're referencing the scripts of the lookup control instead of the subgrid. Here's how you would set the default view of the subgrid dynamically based on your code:

    var OnBoarding = window.OnBoarding || {};
    (
     function () {
     this.setDefaultView = function (executionContext) {
    
     let formContext = executionContext.getFormContext();
     let gridContext = formContext.getControl("rh_geschaftsnummer");
    
     let viewToSet = {
     entityType: 1039, // SavedQuery
     id: "3A282DA1-5D90-E011-95AE-00155D9CFA02", //GUID of default view
     name: "Defaukt" //the name
     };
    
     let officeLocation = formContext.getAttribute("rh_standort").getValue();
     if (officeLocation === "Naila") {
     viewToSet = {
     entityType: 1039, // SavedQuery
     id: "3A282DA1-5D90-E011-95AE-00155D9CF123", //GUID of the view
     name: "Naila"
     };
     } else {
     viewToSet = {
     entityType: 1039, // SavedQuery
     id: "3A282DA1-5D90-E011-95AE-00155D9CFABC", //GUID of the view
     name: "Other" //the name
     };
     }
    
     gridContext.getViewSelector().setCurrentView(viewToSet);
     }
     }
    ).call(OnBoarding)

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 401 Most Valuable Professional

#2
11manish Profile Picture

11manish 201 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard