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

Button goes back to default color after refresh

Button 1 is pressed so it changes the color from blue to yellow and it should stay yellow even if the browser get refreshed. 

But right now if the browser get refreshed the app starts from scratch.!!

 

Any idea's?

2 ACCEPTED SOLUTIONS

Accepted Solutions
RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

We are all learning every day 🙂

Just a caution - this might be a bit tedious depending on how many buttons you want to do this for.  If you can't avoid people refreshing the entire browser then keep reading...

 

You can create a list in your site and call it ButtonStatesList or something.  

Add columns to identify the button and its state - you can use the default Title column for the button and add a single-line-of-text column called "State". 

Back in your PowerApp, add a datasource by clicking on the data icon on the left of the screen and Add Data - find Sharepoint and add a Sharepoint data source.  Then on the right find and select your site and select your ButtonStatesList.  

With your list connected to your app, add a button and change it's name to "Button1" - you can do this by selecting the button and then either on the right where you see it's name above the properties you can edit the name, or if you're using the Tree View on the left just double-click the button in the tree view and you can rename it.  You can call it whatever you want, it won't change anything other than your ability to easily identify it while looking at your app - what's important is what the buttons saves to SharePoint in the Title column that will associate it with it's own state.

Set the OnSelect: property of your button to;

 

 

With({buttonRecord: LookUp(ButtonStatesList, Title="Button1")}, 
//gets the existing button record, if it exists and places it into a variable called "buttonrecord" that we can reuse later in our formula

If(IsBlank(buttonRecord.State), 
//checks if there's an existing record by checking if the state is blank - i.e. nothing has been captured yet

  Patch(ButtonStatesList, Defaults(ButtonStatesList), {Title: "Button1", State: "On"}), 
  //if it is blank, then we assume there's no record yet and create one the first time the button is pressed, and set it's state to "On"

  Patch(ButtonStatesList, buttonRecord, {Title: "Button1", State: If(buttonRecord.State="Off", "On", "Off")}) 
//if it's not blank, then the record already exists so we update the existing record, setting it to "On" if it's currently "Off" and "Off" if it's currently "On"

)

)

 

 

 Now go to your button Fill: property and set it as follows;

 

 

If(LookUp(ButtonStatesList, Title="Button1", State)="On", Yellow, Blue)

 

 

That should do it.  If you add more buttons, just make sure each button is set to check for whatever Title it's capturing when pressed - Just replace the above with "Button2", "Button3" etc. as you go.  Whenever your app is loaded, the button will get it's Fill color from the list.

You should note this will work fine for one person using the app - PowerApps will refresh the state automatically whenever it's updated by that user for that PowerApps session - but nobody else will see the update unless their app is continuously checking for it, or they update the color themselves and force a refresh for their session.  If you want everyone to see state updates whenever someone else updates them, you'll have to add a timer to Refresh() the data source periodically.  To do this, just add a time control and set the following properties;

  • Duration: 15000 (refreshes every 15 seconds)
  • Repeat: On/true
  • AutoStart: On/true
  • OnTimerEnd: Refresh(ButtonStatesList)
  • Visible: false
  • AutoPause: On/true if your buttons only exist on one screen, Off/false if they are spread across multiple screens

You Hope this helps 🙂

RT

View solution in original post

RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

I have some questions - because I don't see how your formula is figuring out which button information it's fetching - and based on your example you need to track the status for three buttons independently.  Is this still what you're trying to do, or did I miss something?

If this is what you're trying to do, then you need to save each button's data and be able to retrieve it, so your buttonState list should have;

  • Three rows with two or three columns - one column for each button that identifies each button uniquely, (initially we were using the Title column) plus at least one column to track clickCount, or two if you're still tracking the button clicked status
Title (Text)clickCount (Number)
Button1 
Button2 
Button3 

Each button should patch it's own Title for future lookups, incrementing it's clickCount.

What I'm seeing in your code is different behaviour;

  • Your lookup is fetching a row from a list called SafetyCross, where the Title is = gblClickCount.  
  • Your Patch statement is likewise patching gblClickCount into the title. 

How is this different for each button?

How are you identifying individual buttons in your Patch or LookUp statements?  

What does this list currently look like?

 

Also, with your Patch, you're saving two identical values - buttonClicks and clickCount.  Are these not the same thing?

 

Admittedly, there are always many ways to achieve the same thing, and you should do what makes you comfortable - but also what makes sense to you.  I built an example using a collection instead of SharePoint, but this should show what I see as your ask - if it's different to this then I'm on the wrong track.

If it's accurate, feel free to download the msapp file attached to see how it was done - you can open it using "Open", "Browse Files" from the maker portal and select the file you downloaded.  Here's what it looks like;

 

buttonClick.gif

 

 

Kind regards,

RT

View solution in original post

17 REPLIES 17
RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

The entire app will restart if the browser is refreshed, and all components will be reset to their default state, so this is unavoidable unless you save the state of the button outside the app - for example in a sharepoint list somewhere - and then load the state when the app starts.

Kind regards,

RT

Anonymous
Not applicable

Hello @RusselThomas 

Thanks for the respons dear.

let me just say that i am very new to powerapps. 

I have a sharepoint list and its connected to the app, i just dont know how to record the button status to a column. 

RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

We are all learning every day 🙂

Just a caution - this might be a bit tedious depending on how many buttons you want to do this for.  If you can't avoid people refreshing the entire browser then keep reading...

 

You can create a list in your site and call it ButtonStatesList or something.  

Add columns to identify the button and its state - you can use the default Title column for the button and add a single-line-of-text column called "State". 

Back in your PowerApp, add a datasource by clicking on the data icon on the left of the screen and Add Data - find Sharepoint and add a Sharepoint data source.  Then on the right find and select your site and select your ButtonStatesList.  

With your list connected to your app, add a button and change it's name to "Button1" - you can do this by selecting the button and then either on the right where you see it's name above the properties you can edit the name, or if you're using the Tree View on the left just double-click the button in the tree view and you can rename it.  You can call it whatever you want, it won't change anything other than your ability to easily identify it while looking at your app - what's important is what the buttons saves to SharePoint in the Title column that will associate it with it's own state.

Set the OnSelect: property of your button to;

 

 

With({buttonRecord: LookUp(ButtonStatesList, Title="Button1")}, 
//gets the existing button record, if it exists and places it into a variable called "buttonrecord" that we can reuse later in our formula

If(IsBlank(buttonRecord.State), 
//checks if there's an existing record by checking if the state is blank - i.e. nothing has been captured yet

  Patch(ButtonStatesList, Defaults(ButtonStatesList), {Title: "Button1", State: "On"}), 
  //if it is blank, then we assume there's no record yet and create one the first time the button is pressed, and set it's state to "On"

  Patch(ButtonStatesList, buttonRecord, {Title: "Button1", State: If(buttonRecord.State="Off", "On", "Off")}) 
//if it's not blank, then the record already exists so we update the existing record, setting it to "On" if it's currently "Off" and "Off" if it's currently "On"

)

)

 

 

 Now go to your button Fill: property and set it as follows;

 

 

If(LookUp(ButtonStatesList, Title="Button1", State)="On", Yellow, Blue)

 

 

That should do it.  If you add more buttons, just make sure each button is set to check for whatever Title it's capturing when pressed - Just replace the above with "Button2", "Button3" etc. as you go.  Whenever your app is loaded, the button will get it's Fill color from the list.

You should note this will work fine for one person using the app - PowerApps will refresh the state automatically whenever it's updated by that user for that PowerApps session - but nobody else will see the update unless their app is continuously checking for it, or they update the color themselves and force a refresh for their session.  If you want everyone to see state updates whenever someone else updates them, you'll have to add a timer to Refresh() the data source periodically.  To do this, just add a time control and set the following properties;

  • Duration: 15000 (refreshes every 15 seconds)
  • Repeat: On/true
  • AutoStart: On/true
  • OnTimerEnd: Refresh(ButtonStatesList)
  • Visible: false
  • AutoPause: On/true if your buttons only exist on one screen, Off/false if they are spread across multiple screens

You Hope this helps 🙂

RT

Anonymous
Not applicable

@RusselThomas 

 

You did it, thank you dear 👍

Anonymous
Not applicable

 

Hello @RusselThomas 

Could you help me again please? 

I have these 3 buttons that changes 7 different colors one color pr. click. i want to patch button values to my sharepoint list so when i restart the app the buttons stays at same color and dont go back to default. Just the same like the first problem but now the buttons changes between 7 colors. 

 

 

 

 

 

 

The code is for each button: 

On.Select

If(gblClickCount<7;Set(gblClickCount;gblClickCount+1);Set(gblClickCount;1));;

 

 

 Fill.
Switch(gblClickCount;
 
1; Green;
 
2; Yellow;
 
3; SkyBlue; 
 
4; Orange;
 
5; MediumPurple;
 
6; Red;
 
Capture1.PNG
RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

So at this point, the user experience becomes a critical deciding factor in how you take it forward.

Do you want each user to keep track of their own button clicks, or are the button states universal (the same) for all users?  i.e. if I click button 3 through to Orange and you're running the app, do you want to see button 3 change to Orange?  Or do you want to track your own button clicks independent from mine?

Kind regards,

RT

Anonymous
Not applicable

@RusselThomas 

Thank you for your respons.

All users should be the same there is only one display for this app. I just dont want to lose any data when the app gets refreshed in the web browser . 

 

Best regards 

 

 

RusselThomas
Community Champion
Community Champion

Hi @Anonymous ,

That makes it easier, as you're already saving a row of data for each button 'state'.

All you need to do is add a number column to your button state list - call it 'buttonClicks' or something that makes sense to you - this is where you can store the number of clicks the button is on.  Remember to refresh the data source in PowerApps after you've added the column to SharePoint ("Data" menu, "..." next to your data source, "Refresh")

If we take a step back and look at the solution, it may make more sense to keep your number/colour reference as a lookup table than just existing in logic.  By this I mean, instead of switching the value of ClickCount for each button to get the fill colour, it would be better to store the clickcount/fillcolour in a table and just look it up.

This way, if you want to change the colour of a number, you don't have to go and edit three buttons worth of code to do it.

There is a behaviour called App Start you can use to initialise information for your app.  This will run exactly once, when your app starts.  You can put any behaviour functions in app start, including collecting data from a source, setting variables, or in this case, creating a reference table for your app to use.

On your left navigation menu, above your first screen (usually Screen1), you should see "App". Click on this, and then make sure your formula bar property is set to OnStart (the down arrow to the left of the fx on the formula bar).

This can go in your OnStart: property;

 

ClearCollect(collectClickColours; {
  numClicks: 1; fillColour: Green;
  numClicks: 2; fillColour: Yellow;
  numClicks: 3; fillColour: SkyBlue;
  .....
  });;

 

So here we'll set up our collection called collectClickColours with a column "numClicks" and corresponding "fillColour" (you can call the columns what you like, as long as it makes sense to you - just replace the ..... with the rest of the numbers and colours - I'm lazy 🙂)

Our buttons will reference this table to get their colours - and if we want to change a numbers colour, we only have to do it here, once.

 

Now your buttons' Fill: properties can look as follows;

 

LookUp(collectClickColours;
     numClicks = LookUp('buttonStatesList'; Title='Button1'; clickCount); fillColour
)

 

 If there's only one screen/user, you don't have to worry about refreshing - and if the app 'reboots', then it should fetch the last saved click counts.

[Edit]

I forgot to add, you'll want to include the clickcount incrementer in your button OnSelect: patch update.  Editing the code from the original post, it would look something like this;

With({buttonRecord: LookUp(ButtonStatesList, Title="Button1")}, 

If(IsBlank(buttonRecord.State), 

  Patch(ButtonStatesList, Defaults(ButtonStatesList), {Title: "Button1", State: "On", buttonClicks: 1}), 
  
  Patch(ButtonStatesList, buttonRecord, {Title: "Button1", State: If(buttonRecord.State="Off", "On", "Off"), buttonClicks: buttonRecord.buttonClicks+ 1})
)

)

Try it out and let me know how it goes 🙂

Kind regards,

RT

Anonymous
Not applicable

Hello @RusselThomas

Its recording to the sharepoint list but i getting error when i am adding the code OnStart: 2.PNG 

 

Helpful resources

Announcements

Celebrating the May Super User of the Month: Laurens Martens

  @LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!        

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!      

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Community Roundup: A Look Back at Our Last 10 Tuesday Tips

As we continue to grow and learn together, it's important to reflect on the valuable insights we've shared. For today's #TuesdayTip, we're excited to take a moment to look back at the last 10 tips we've shared in case you missed any or want to revisit them. Thanks for your incredible support for this series--we're so glad it was able to help so many of you navigate your community experience!   Getting Started in the Community An overview of everything you need to know about navigating the community on one page!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Ranks and YOU Have you ever wondered how your fellow community members ascend the ranks within our community? We explain everything about ranks and how to achieve points so you can climb up in the rankings! Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Powering Up Your Community Profile Your Community User Profile is how the Community knows you--so it's essential that it works the way you need it to! From changing your username to updating contact information, this Knowledge Base Article is your best resource for powering up your profile. Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Blogs--A Great Place to Start There's so much you'll discover in the Community Blogs, and we hope you'll check them out today!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Unlocking Community Achievements and Earning Badges Across the Communities, you'll see badges on users profile that recognize and reward their engagement and contributions. Check out some details on Community badges--and find out more in the detailed link at the end of the article! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Blogging in the Community Interested in blogging? Everything you need to know on writing blogs in our four communities! Get started blogging across the Power Platform communities today! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Subscriptions & Notifications We don't want you to miss a thing in the community! Read all about how to subscribe to sections of our forums and how to setup your notifications! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Getting Started with Private Messages & Macros Do you want to enhance your communication in the Community and streamline your interactions? One of the best ways to do this is to ensure you are using Private Messaging--and the ever-handy macros that are available to you as a Community member! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Community User Groups Learn everything about being part of, starting, or leading a User Group in the Power Platform Community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Update Your Community Profile Today! Keep your community profile up to date which is essential for staying connected and engaged with the community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Thank you for being an integral part of our journey.   Here's to many more Tuesday Tips as we pave the way for a brighter, more connected future! As always, watch the News & Announcements for the next set of tips, coming soon!

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Top Solution Authors
Top Kudoed Authors
Users online (2,970)