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

Using a toggle to show/hide inputs and update data

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@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?

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

3 REPLIES 3
RandyHayes
Super User
Super User

@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?

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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! 👍

RandyHayes
Super User
Super User

@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.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,561)