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

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / OnStart Switch stateme...
Power Apps
Unanswered

OnStart Switch statement fail - Global variable not being set based on switch statement

(1) ShareShare
ReportReport
Posted on by 34
Hello,
 
I have a globa variable called "var_NamingLevel". I use it as a show/hide parameter for 3 galleries that i have. One gallery shows 1 letter code list, another one 2 letter code and the third one for 3 letter code list. Users can use buttons at the top to change the gallery that they want to see. Pretty straight forward.
I also have App OnStart property set:
 
Switch(
    gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level',
    1,
    Set(
        var_NamingLevel,
        1
    ),
    2,
    Set(
        var_NamingLevel,
        2
    ),
    3,
    Set(
        var_NamingLevel,
        3
    ),
    Set(
        var_NamingLevel,
        1
    )
);
 
For the sake of sanity, i did a text output of the said variable, to see if it was correctly applied on start. Here is the code: 
"Current value of var_NamingLevel: " & var_NamingLevel & ", Object: " & gal_Objects.Selected.'Object Name' & " has classification level: " & gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level'
 
What is am doing here is getting the row from the row from Dataverse model driven app. I'm checking what is current classification level. Printout correctly returns that it has classification level 2 (intiger type data). But OnStart switch statement doesn't seem to work. It defaults to 1. 
 
Categories:
I have the same question (0)
  • Suggested answer
    mmbr1606 Profile Picture
    14,560 Super User 2025 Season 2 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    hey
     
     
    just a question, did u test it while buildung the app? if so did u manually run the onstart function so the code gets triggered?
     
    maybe try the onvisible of screen instead:
    Switch(
        gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level',
        1,
        Set(var_NamingLevel, 1),
        2,
        Set(var_NamingLevel, 2),
        3,
        Set(var_NamingLevel, 3),
        Set(var_NamingLevel, 1)
    )
    
    if this helped please mark as verified answer
     
     
    cheers
  • MatevzP Profile Picture
    34 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    @mmbr1606 I did test it in app (preview). Didn't work. I also tried your solution to put it on screen property OnVisible. It also doesn't work. Well, partially. Once the app is loaded and i land on that screen, it doesn't work. Switch statement defaults to whatever default value i have set. But if i create a new screen for testing purposes, and i switch back and forward, then "OnVisible" switch statement kicks in. 
    So: on app load, when my main (and only screen) shows up, OnVisible swithc statement doesn't work. But if i switch away to test screen and then back, it works.
    Hmm...
  • msoenmez Profile Picture
    46 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    When you are setting the var_NamingLevel inside OnStart Eventgal_Objects is probably not fully loaded and Default property of the Gallery is not yet set. 
     
    But, if you use gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level' directly in a non-function property, it will always get updated by the changes in gal_Objects and reflect them directly in the UI.
     
    OnStart runs parallel with the OnVisible function of the Default App Page and the data load, if you not configured it otherwise in the app settings. So, you cannot be sure, if OnStart executes after your gallery gal_Objects loads the items and sets the Default property.
     
    I would suggest you, that you use gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level' directly in the part where you want to show this value or use a hidden TextBox component with default value of gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level' and add your Switch logic inside the OnChange method of the TextBox.
     
  • MatevzP Profile Picture
    34 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    @msoenmez Thank you for the input!
    I thought it probably had something to do with it being imperative? (maybe i'm wrong on the reason :D)
     
    Anyhow, i tried with a drop down control. I did following code on OnChange event of the drop down: 
    Set(var_NamingLevel, Self.Selected.'Clas>sification Level')
     

    Control showed the value of the drop down was set correctly, but it didn't change the global variable for some reason.

     
    I also tried by adding that switch statement, but to no avail. Global Var is simply left empty.

    Actually, on change doesn't work at all for some reason...
     
  • msoenmez Profile Picture
    46 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    Can you please show what you have in the DefaultSelectedItems and Items property of the Dropdown?
     
    And also can you show me in the left navigation the Variables tab, which context and global variables you have? I ask this, because I suspect you may have used Set(var_NamingLevel,"XYZ) to set the variable globally but if you have a context variable in this page with the same name (set with UpdateContext({var_NamingLevel:"ABC"})), it will refer to the context variable, not to the global variable .
     
     
  • MatevzP Profile Picture
    34 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    Can you please show what you have in the DefaultSelectedItems and Items property of the Dropdown?
     
    And also can you show me in the left navigation the Variables tab, which context and global variables you have? I ask this, because I suspect you may have used Set(var_NamingLevel,"XYZ) to set the variable globally but if you have a context variable in this page with the same name (set with UpdateContext({var_NamingLevel:"ABC"})), it will refer to the context variable, not to the global variable .
    Drop down properties:
     
    Items: gal_Objects.Selected.'Related RDS Object Naming'
     
    Default: 
    gal_Objects.Selected.'Related RDS Object Naming'.'Classification Level'
     
    OnChange:
    Set(var_NamingLevel, Dropdown1.Selected.'Classification Level')
     
     
    No local variables set
  • timl Profile Picture
    36,319 Super User 2025 Season 2 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    Hi @MatevzP
     
    I suspect this might be timing issue based on this the canvas app is embedded in a model driven app.
     
    I'm not sure if this still works, but you could experiment with the 'toggle' workaround. 
     
     
  • MatevzP Profile Picture
    34 on at
    OnStart Switch statement fail - Global variable not being set based on switch statement
    Hey, timl, i can't tag your name, i couldn't find it in the tag list.
     
    Your suggestion worked. Not actually the toggle trigger that that blog suggest, but his old solution with the timer worked for me! :)
     
    Thank you and @msoenmez @mmbr1606 for help! 

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 757 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 322 Super User 2025 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 209 Super User 2025 Season 2

Last 30 days Overall leaderboard