cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Anonymous
Not applicable

Assigning bool to a variable problem

Hi all

 

Have some stange issue - cannot assign boolean or text value to a variable, e.g. if user from particular departmant opens the app then in the label:

 

if comparison is in the label then:

 boolVar1.png

if first set variable in screen OnVisible 

boolVar2.png

and then put var in the label:

boolVar3.png

 

Moreover:

boolVar5.pngboolVar4.png

 

This is my second app, in the first one this kind of assignment seemed to work ( a-la Set(var,"Bob") - for sure). The only difference is that this app is for mobile and the first was for bigger screen.

 

Thx in advance for any help!

 

 

Best,

 

Nick

 

1 ACCEPTED SOLUTION

Accepted Solutions
Meneghino
Level 10

Re: Assigning bool to a variable problem

Thanks for clarifying the issue.

The problem is not related to variable types.  The OnVisible action is fired when the screen is navigated to.

Hence if you Set(varA, "Bob") or Set(varB, 1=1) all that it does is to define varB as text/boolean, the value will be null/blank.

Only when the screen is navigated to (including when the app is opened, or navigated to from another screen) is the value actually assigned.

https://docs.microsoft.com/en-us/powerapps/controls/control-screen

You can verify this by creating a button instead and set its OnSelect property.  The value should be null/false until the button is first pressed.

By the way, the lack of a proper treatment of null values in PowerApps is annoying, varB is null initially and not false.  You can test this by creating a label with text IsBlank(varB).  You will see that it is null, but varB will still show as false.

PowerApps is messy, be prepared.

10 REPLIES 10
Meneghino
Level 10

Re: Assigning bool to a variable problem

PowerApps is strongly typed, hence a variable is either boolean or text but not both.

Unfortunately MS decided to omit variable type declaration (presumably because they wanted to market PowerApps as a no-code product) so that if you try to set the same variable to different types, then one or both of the Set statements should throw an error in the designer.  If an error is thrown then the result is a null value.

I hope I have understood your issue correctly and addressed it.

 

Anonymous
Not applicable

Re: Assigning bool to a variable problem

hi

 

I guess it is a bit of misunderstanding - I was talking about alternatives, not consequent actions. So, if I assigned only boolean value to a variable, first time, it just didn't work. And the same if I did for text value.

 

Small update - it seems that your answer actually partly explains the problem for me; here are consequent steps you may want to follow to reproduce:

a. On a screen OnVisible=Set(varA, "Bob") [I assume now it is the moment of variable type declaration, no?]

b. Insert a label and Text=varA [I expect label to say "Bob" now]

c. On the same screen replace OnVisible with Set(varB, 1=1) [so, I actually declare a new variable, of a new type...]

d. In the same label replace Text with varB [result - label shows "false" though I would expect it show true].

 

Now, if I close the app and reopen it the label will actually show "true". So, it seems to me like issue is with type declaration but PowerApps just.. mmm.. remembers that previous var here was a text, so next var in the same place is also a text and then it does not work because I try to assign bool there. And PowerApps revisions types only after app reboot. ( crazy theory, I know, sorry, not a programmer Smiley Happy

Meneghino
Level 10

Re: Assigning bool to a variable problem

Thanks for clarifying the issue.

The problem is not related to variable types.  The OnVisible action is fired when the screen is navigated to.

Hence if you Set(varA, "Bob") or Set(varB, 1=1) all that it does is to define varB as text/boolean, the value will be null/blank.

Only when the screen is navigated to (including when the app is opened, or navigated to from another screen) is the value actually assigned.

https://docs.microsoft.com/en-us/powerapps/controls/control-screen

You can verify this by creating a button instead and set its OnSelect property.  The value should be null/false until the button is first pressed.

By the way, the lack of a proper treatment of null values in PowerApps is annoying, varB is null initially and not false.  You can test this by creating a label with text IsBlank(varB).  You will see that it is null, but varB will still show as false.

PowerApps is messy, be prepared.

Meneghino
Level 10

Re: Assigning bool to a variable problem

PS the mess is even starker if you Set(varA, "")

For PowerApps ""=Blank() is not consistent but this is clearly not the case for any database system that PowerApps will need to converse with.

 

Also, try this in a label:

IsBlank("") will give you true but ""=Blank() will give you false

Super User
Super User

Re: Assigning bool to a variable problem


@Anonymous wrote:

hi

 

I guess it is a bit of misunderstanding - I was talking about alternatives, not consequent actions. So, if I assigned only boolean value to a variable, first time, it just didn't work. And the same if I did for text value.

 

Small update - it seems that your answer actually partly explains the problem for me; here are consequent steps you may want to follow to reproduce:

a. On a screen OnVisible=Set(varA, "Bob") [I assume now it is the moment of variable type declaration, no?]

b. Insert a label and Text=varA [I expect label to say "Bob" now]

c. On the same screen replace OnVisible with Set(varB, 1=1) [so, I actually declare a new variable, of a new type...]

d. In the same label replace Text with varB [result - label shows "false" though I would expect it show true].

 

Now, if I close the app and reopen it the label will actually show "true". So, it seems to me like issue is with type declaration but PowerApps just.. mmm.. remembers that previous var here was a text, so next var in the same place is also a text and then it does not work because I try to assign bool there. And PowerApps revisions types only after app reboot. ( crazy theory, I know, sorry, not a programmer Smiley Happy


 

That behavior should result in a red squigly line under the error value with a yellow flag on it. There's some built in heirarchy to how PowerApps picks it's variable declaration too that if you update a variable on another screen the behavior you have been seeing happens. When it does, the solution I've used is to just simply cut/paste out the variable and for whatever reason it fixes the problem each time.

 

Did you get your issue resolved?

Anonymous
Not applicable

Re: Assigning bool to a variable problem


@Meneghino wrote:

Thanks for clarifying the issue.

The problem is not related to variable types.  The OnVisible action is fired when the screen is navigated to.

Hence if you Set(varA, "Bob") or Set(varB, 1=1) all that it does is to define varB as text/boolean, the value will be null/blank.

Only when the screen is navigated to (including when the app is opened, or navigated to from another screen) is the value actually assigned.

https://docs.microsoft.com/en-us/powerapps/controls/control-screen

You can verify this by creating a button instead and set its OnSelect property.  The value should be null/false until the button is first pressed.

By the way, the lack of a proper treatment of null values in PowerApps is annoying, varB is null initially and not false.  You can test this by creating a label with text IsBlank(varB).  You will see that it is null, but varB will still show as false.

PowerApps is messy, be prepared.


 

Thx for the clarification! It is clear now what I was doing wrong.

Anonymous
Not applicable

Re: Assigning bool to a variable problem

btw, shouldn't OnVisible also work in preview then? I have OnSelect=Set(varA, 1=1) on the first screen of the app, and when I press preview it still does not work (I have false, or null as you say), BUT if I switch from the first screen to the second and then go back to the first - OnSelect fires and variable is assign normally - to true.

 

 

Meneghino
Level 10

Re: Assigning bool to a variable problem

As you have discovered the OnVisible is not fired by just pressing play (i.e. going from design to preview in the same screen).  Interestingly it is (or at least used to be) fired when switching between screens in design mode.

Super User
Super User

Re: Assigning bool to a variable problem


@Anonymous wrote:

btw, shouldn't OnVisible also work in preview then? I have OnSelect=Set(varA, 1=1) on the first screen of the app, and when I press preview it still does not work (I have false, or null as you say), BUT if I switch from the first screen to the second and then go back to the first - OnSelect fires and variable is assign normally - to true.

 

 


 

That's correct, OnVisible runs the first time the screen is viewed so just opening the screen in the editor will immediately run those. Pretty odd that the Play button doesn't act like a first time view and we have to navigate away and back... MS Access got it right for editing and viewing immediately.

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Community Contributors for July 2019

Top Community Contributors for July 2019

Let's thank our top community contributors

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

FifthImage

Dynamics 365 and Power Platform April 2019 Release notes

Features releasing from April 2019 through September 2019!

Users Online
Currently online: 191 members 4,962 guests
Please welcome our newest community members: