Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

Using a toggle to show/hide inputs and update data

(0) ShareShare
ReportReport
Posted on by 92

Hi,

 

I'm fairly new to power apps, with this being my first proper attempt at a real-world app. Unfortunately, I've encountered a problem that has had me stumped for hours. Hopefully I can explain this well enough that you could help me solve it.

 

I am creating a custom timesheet solution for our company. Occasionally an employee might need to spend 30 minutes clearing out their van at home or carrying out some other task that doesn't fit into their workday. In these cases, the employee should still "clock out" when they leave the site, but they have a toggle switch option called "Additional Time" which, if set to true, an additional 2 inputs appear "Additional Time Amount" for them to record the amount of time, and "Additional Time Reason" where they write why they required the additional time.

 

The editform when "no" is selected for additional timeThe editform when "no" is selected for additional time

 

the edit form when "yes" is selected for additional timethe edit form when "yes" is selected for additional time

 

I've been through a few hurdles with this solution so far, but I feel like I'm finally nearing the finish line. Here's what I've done so far:

 

  • If the user toggles Additional Time to "Yes" then I want the 2 additional inputs to be displayed. If it's set to "No" then I want them hidden. I have added this code to the "Visible" property of the data cards for the 2 inputs (Additional Time Amount, Additional Time Reason):

 

Toggle6.Value

 

 

 

  • If the user changes the toggle (Additional Time) to "Yes", and fills information into the 2 related inputs (Additional Time Amount, Additional Time Reason), but then changes the toggle to "no" (perhaps they made a mistake) then I don't want the app still sending the data for "Additional Time Amount" and "Additional Time Reason", so I added the following code to clear the inputs using the "OnUncheck" for the Toggle:

 

Reset(DataCardValue5);
Reset(DataCardValue6);

 

 

  • If the user is not entering additional time, and the "Additional Time Amount" and "Additional Time Reason" inputs are hidden, then I do not want them to be "Required", on the other hand, if they toggle "Yes" then I do want the 2 additional inputs to be required, so I added this code to the 2 inputs under "Required":

 

If(
 Toggle6.Value = true,
 true,
 false
)

 

 

At this point I thought, this is great, working perfectly! But after some testing, I realised I had a few more things to solve.

 

If the user submits a timesheet that has the toggle for "Additional Time" set to "Yes" and has filled in the "Additional Time Amount" and "Additional Time Reason" but later goes back and changes the "Additional Time" toggle to "No", the data in the "Additional Time Amount" and "Additional Time Reason" inputs still remain on the table, even though the "Additional Time" field states "No", to solve this (which took hours of googling) I came up with the below mishmash of code from various resources, this code was added to the "OnUncheck" of the "Additional Time" toggle :

 

 

Reset(DataCardValue5);
Reset(DataCardValue6);
Patch(
 Timesheets,
 LookUp(
 Timesheets,
 ID = DataCardValue8.Text
 ),
 {
 'Additional Time Amount': Blank(),
 'Additional Time Reason': Blank(),
 'Additional Time': false
 }
);

 

 

I think I've hit it out of the park, it's working perfectly! Except it isn't. This is a real outlier, but bare with me:

 

  1. The user submits timesheet with "Additional Time" toggled "Yes" and fills in the "Amount" and the "Reason" - Works perfectly
  2. The user made an error, they entered "Additional Time" for the wrong date, so they go in and toggle "Additional Time" to "No" - Works perfectly, data is cleared from both inputs and the "Additional Time" field is set to false
  3. The user realised that they DID mean to have additional time on this timesheet (now we're at 1 submission and 2 edits). So they go back in to edit the timesheet, flick the toggle to "Yes" and enter data into the 2 additional fields, they click save and this happens: https://streamable.com/3fx0kh (video)

If you notice, when I hit "Save" about 27s into video and it loads the "detail screen" the data I entered briefly flashes up but then disappears. It is not saved in the table either.

 

Sorry for the long post, if you have any insight as to why this is happening I would be very grateful for your advice! If I've done this a horrible way and you think there is a better way then I am also open to suggestions!

 

Categories:
  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: Using a toggle to show/hide inputs and update data

    @omainesi 

    Very good!  Yes, I see a lot of people that use Patch to patch a form and they don't need to (and in fact, you lose most all of the features of a Form when you do).  There is no need.  Forms are very flexible in their operation as long as you know all the places to do things.

    I'd recommend to carve some time out and watch my video on Forms Plus - everything you ever wanted to know about EditForm and some ways to use them that you may not have even know.  It's a long one, but very comprehensive.  You will see that when you use them, things become much simpler. 

     

    Just post back here when you have issues.  Don't spend too much time chasing ghosts.

     

  • Omainesi Profile Picture
    92 on at
    Re: Using a toggle to show/hide inputs and update data

    Thank you for the quick, detailed reply Randy.

     

    I have made the changes you have mentioned (apart from changing the reset on toggle - I'll be looking into how to change this to be considered "on submit" this evening), and everything is working perfectly!

     

    In regards to using patch - this is what happens when you've spent 3 hours trying to fix something that you don't entirely understand, and have turned to desperately scouring google at 2am for something that could help. I have a lot more to learn, starting with a deeper dive into forms!

     

    Thanks again! 👍

  • Verified answer
    RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on at
    Re: Using a toggle to show/hide inputs and update data

    @omainesi 

    A couple things to consider:

    1) Don't reset your Additional Time and Amount columns on the toggle.  This could be frustrating to a user because they might have entered something, then toggled the additional time off by mistake, turn it back on and all is gone.  Instead consider this as the submit time.  This would also resolve the issue of editing a record.

    2) Why are you using the Patch statement if this is a form?  You should be using SubmitForm on the form.

     

    Now:

    - Change the Required property on the two additional datacards to Toggle6.Value You don't need all the If true true false stuff.  The toggle will either be true or false already.

    - On the Update property of the two datacards, set the formula to the following:

    If(Toggle6.Value, 
     yourDataCardValuexControlName.Text,
     Blank()
    )

    You WILL need to make sure that the Formula level error Management feature is turned ON in your advanced settings for this to work.

    Also, as I give the above formula as a general formula you need for your Update property, you also appear to have a dropdown in the one column, so the actual formula will need to be adjusted to that control and to the underlying data column type.

    - Discard using Patch and use SubmitForm on your form : SubmitForm(yourForm)

     

    As for the item disappearing in the detail, the question would be, for both of the EditForm and the Display form, what is the Item property?

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,524 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,906 Most Valuable Professional

Leaderboard