Prevent overlapping of entered time by the user.

     Usecase: User wants to clock in his claims, and for this he has to lock in his time period for work on that claim. The system should validate if this timeperiod has already been entered by him or not, and hence prevents him/her for entering overlapping timeperiod claims.

Solution:

I tried various approaches to begin with, like saving a list with 24 fields each representing one hour. And then when claim was punched in the flow was to update the time period for that claim. This was just a basic solution and was rigid. Also it could not handle claims if the timeperiod was in hours and minutes like 1hr 30 mins for a claim. 

So i needed something which was more dynamic and flexible in the approach. Also, i am a firm believer of the fact, that if something is incorrect the best solution is to refrain user from entering it inside the system. That way, you dont have to deal with fixing with it later. Yup i know i am lazy.

The below solution leverages collections. First i create a collection on submission of a claim.
The buttons like create col, copy and clear are used only for testing purpose and can be removed from the final solution.





Below is the code for Clear Col Button

Collect(

    colSelectedTimeSlots,

    {

        StartTime: Time(

            Value(Dropdown1.SelectedText.Value),

            Value(Dropdown1_1.SelectedText.Value),

            0

        ),

        EndTime: Time(

            Value(Dropdown2.SelectedText.Value),

            Value(Dropdown2_1.SelectedText.Value),

            0

        )

    }

);

All this does is creates a collection colSelectedTimeSlots for the first time the user enters the claim, you can even move this code to Submit Claim.

Copy button is used to copy the collection to another collection. The onSelect property for this button says

Collect(colTemp,colSelectedTimeSlots);

where colTemp is the copy of parent collection.

Clear button is to reset these collections. its OnSelect property says like this 
Clear(colTemp); Clear(colSelectedTimeSlots);

We have a gallery where in we showing the data inside the main collection colSelectedTimeSlots.

So what we want is, compare the timeperiod for the claim with all the values in the colleciton. And if the timeperiod is overlapping, then we will disable the submit claim button, and if the value is valid we will allow the user to save the claim into the collection/gallery.

Below is the code snippet i have on the DisplayMode for SubmitClaim button.

If(
    CountIf(
        ForAll(
            colTemp,
            If(
                Time(
                    Value(Dropdown1.SelectedText.Value),
                    Value(Dropdown1_1.SelectedText.Value),
                    0
                ) > StartTime && Time(
                    Value(Dropdown1.SelectedText.Value),
                    Value(Dropdown1_1.SelectedText.Value),
                    0
                ) < EndTime Or Time(
                    Value(Dropdown2.SelectedText.Value),
                    Value(Dropdown2_1.SelectedText.Value),
                    0
                ) > StartTime && Time(
                    Value(Dropdown2.SelectedText.Value),
                    Value(Dropdown2_1.SelectedText.Value),
                    0
                ) < EndTime Or Time(
                    Value(Dropdown1.SelectedText.Value),
                    Value(Dropdown1_1.SelectedText.Value),
                    0
                ) < StartTime && Time(
                    Value(Dropdown2.SelectedText.Value),
                    Value(Dropdown2_1.SelectedText.Value),
                    0
                ) > EndTime,
                {Value: 1},
                {Value: 0}
            )
        ),
        Value > 0
    ) >= 1,
    Disabled,
    Edit
)

What this code does is that it compares the clocked in values to the StartTime and EndTime values in the collection. 
And if this is true that the value become 1 else it become 0. Now since this if sits pretty inside the ForAll so it loops across the items in collection and compares them to the claim entered timeperiod.

This then results in a table, to which we apply a countif condition. this gives us the count of records which result true for the comparasion.

And then we surround it with an if to check if its value >=1. If this is true, then we Disable the button for the user to submit the claim or else the display mode for this button is set to edit, so that user can submit his/her claim.

So, to cut long story short for a timeperiod which hasn't been registered previously for the claim this countif should return zero. Just like the below screenshot.


Happie PowerAppin!

Comments

Popular posts from this blog

Creating Nested Galleries in Power Apps. (Parent-Child Relationship)

How to reset a datepicker control in PowerApps?

Reset () function failing to work on Edit form for Combo boxes in Power App.