cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper II
Helper II

Button DisplayMode.Disable to DisplayMode.Edit not working as expected

Hello all,

 

I have a button on my home screen whose OnSelect property sends a report in an email. I want the button to be disabled once pressed to indicate to the user that the report is sent and to keep them from sending multiples. To the OnSelect property of the button I've added:

 

...;UpdateContext({sentreport:true})

The DisplayMode property, then:

 

If(sentreport,Disabled,Edit)

 

I want the button to be selectable again if the user adds information to their report. On another screen (inspection screen), I have an icon which is used to submit a form and then navigate away from the screen. I added the following to the OnSelect property of the icon:

 

UpdateContext({sentreport:false})

I've also tried:

 

Set(sentreport, false)

Neither code results in the button's DisplayMode property resetting to DisplayMode.Edit and being selectable again.

 

I'm not sure if this matters, but the screen which adds to the report (inspection screen) navigates to a selection screen, not the home screen with the button in question. Perhaps my sentreport variable isn't carried over from the inspection screen?

 

Any idea why my code isn't working?

1 ACCEPTED SOLUTION

Accepted Solutions
Microsoft
Microsoft

I'm not sure, but it sounds like you're running into an issue with the scoping of context variables. UpdateContext creates variables that only exist on that screen. If I have an UpdateContext({sentreport: true}) on one screen and an UpdateContext({sentreport: false}) on another screen, than each screen has its own sentreport variable. In other words, there will be two different variables with the same name. You can pass context variables through the Navigate function, though, so you can use the value in another screen.

 

However, I recommend that in your case you just use the Set() function. Set(sentreport, false) will create a global variable that can be used on all screens.

 

You may be wondering why Set() wasn't working in your example. This is because if I have both Set(sentreport, false) and UpdateContext({sentreport: true}), the app has two variables with the same name, again. In this case, though, it is a global variable and a local context variable. In cases like this, screens will use the local variable where it's available and the global variable where it isn't. If you replace all of the UpdateContext's with Set() in your case, you should be fine.

 

One feature you may find useful when trying to figure out these kinds of problems is the variables page.

VariablesPage.png

 

If you want to learn more about variables in PowerApps, here are some links to the documentation:

UpdateContext documentation: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-updatecontext 

Set() documentation: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-set

 

I hope this helps!

 

 

 

 

- Paul C.
Software Engineer on PowerApps

View solution in original post

2 REPLIES 2
Super User III
Super User III

Hi @Jdscott

 

The correct function to use is Set, because the scope of any variables that we define with UpdateContext is limited to the screen.

 

To avoid any ambiguity, you'll need to remove any call to UpdateContext on your main screen that sets the sentreport variable.

 

On the button that sends the report, you would add the formula...

...;Set(sentreport,true)

And on the inspection screen, you would use the formula:

;Set(sentreport,false)

 

Microsoft
Microsoft

I'm not sure, but it sounds like you're running into an issue with the scoping of context variables. UpdateContext creates variables that only exist on that screen. If I have an UpdateContext({sentreport: true}) on one screen and an UpdateContext({sentreport: false}) on another screen, than each screen has its own sentreport variable. In other words, there will be two different variables with the same name. You can pass context variables through the Navigate function, though, so you can use the value in another screen.

 

However, I recommend that in your case you just use the Set() function. Set(sentreport, false) will create a global variable that can be used on all screens.

 

You may be wondering why Set() wasn't working in your example. This is because if I have both Set(sentreport, false) and UpdateContext({sentreport: true}), the app has two variables with the same name, again. In this case, though, it is a global variable and a local context variable. In cases like this, screens will use the local variable where it's available and the global variable where it isn't. If you replace all of the UpdateContext's with Set() in your case, you should be fine.

 

One feature you may find useful when trying to figure out these kinds of problems is the variables page.

VariablesPage.png

 

If you want to learn more about variables in PowerApps, here are some links to the documentation:

UpdateContext documentation: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-updatecontext 

Set() documentation: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-set

 

I hope this helps!

 

 

 

 

- Paul C.
Software Engineer on PowerApps

View solution in original post

Helpful resources

Announcements
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

Top Solution Authors
Top Kudoed Authors
Users online (17,526)