cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
duncanp
Level: Powered On

Very odd behaviour with an if statement and Patch/Remove

Hi

I've got a really odd one here.

So I've got a gallery which has as its items a set of fixed values, in this case dates. The reason for this is so that it can display all possible dates in the range within the gallery, even if there is no data in the corresponding SharePoint list for that date. So something like this:

Week BeginningDays Booked
10/02/20203
17/02/20204
24/02/2020 
02/03/20204

For each of the days booked, I've added a text box to the gallery where the user can enter a number against the week. In the example above, there would only be three records in the SharePoint list, even though the gallery shows 4, because as I said above I'm using the dates to drive the gallery, and going to the SharePoint list to look that date up and pull back the days booked if there are any.

So far so normal.

The issue comes when I want to save. I'm using ForAll across the gallery with Patch to save each value. In doing so I want to do three things: update a list item if it's there already; add a new one if it's not; remove it if I blank the days booked text box.

The code I came up with to do this looks like this:

 

ForAll(
        galBookingGallery.AllItems,
        If(
            Value(txtDaysBooked.Text) > 0, //check if the text box has a positive value
            If(
                IsBlank(LinkedBooking), // check if there's already a list item
                Patch(
                    [@BookingList],
                    Defaults([@BookingList]),
                    {Days: Value(txtDaysBooked.Text),
                     WeekBeginning: WeekBeginning}
                ),
                Patch(
                    [@BookingList],
                    LinkedBooking,
                    {Days: Value(txtDaysBooked.Text)}
                )
            ),
            If(
                Not(IsBlank(LinkedBooking)),
                Remove(
                    [@BookingList],
                    LinkedBooking
                )
            )
        )
    );

 

The LinkedBooking is the booking fetched for the gallery item if there is one that matches the gallery date, The BookingList is the SharePoint list, and the txtDaysBooked is the text box for entering the booked days.
However, this gives me red lines under everything and complains "Invalid argument type (Table). Expecting a record value instead".

But if I take the Remove section and move it to the first part of the outer if statement instead of the else part:

 

ForAll(
        galBookingGallery.AllItems,
        If(
            Coalesce(Value(txtDaysBooked.Text), 0) = 0, //check if the text box is blank or zero
            If(
                Not(IsBlank(LinkedBooking)),
                Remove(
                    [@BookingList],
                    LinkedBooking
                )
            ),
            If(
                IsBlank(LinkedBooking),
                Patch(
                    [@BookingList],
                    Defaults([@BookingList]),
                    {Days: Value(txtDaysBooked.Text),
                     WeekBeginning: WeekBeginning}
                ),
                Patch(
                    [@BookingList],
                    LinkedBooking,
                    {Days: Value(txtDaysBooked.Text)}
                )
            )
        )
    )

 

Then it works fine!

I'm sure I'm missing something here, but I can't see any obvious reason why the second version would work but the first wouldn't. Can anyone enlighten me?

 

Duncan

2 REPLIES 2
Microsoft v-siky-msft
Microsoft

Re: Very odd behaviour with an if statement and Patch/Remove

Hi @duncanp 

Could you please share the definition and value of the LinkedBooking? Is it a Variable, Collection or a Column of 'BookingList'?

From the error message and the codes, I think the issue is caused by 'LinkedBooking'.

Note: If you want to remove the records from the list, the syntax should be: 

Remove( DataSource, Record1 [, Record2, ... ] [, All ] )

or
Remove( DataSource, Table [, All ] )

Reference: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-remove-removeif 

Please share more info with  'LinkedBooking', then I can provide further assistance.

Sik

 

 

duncanp
Level: Powered On

Re: Very odd behaviour with an if statement and Patch/Remove

Hi @v-siky-msft 

 

The LinkedBooking is the record of the actual booking in the list that corresponds to the week beginning value. So the gallery in question is populated with the items property as follows:

AddColumns(
    [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11
    ],
    "LinkedBooking",
    LookUp(
        BookingList,
        WeekBeginning >= DateAdd(
            locStartDate,
            Value * 7
        ) And WeekBeginning <= DateAdd(
            locStartDate,
            Value * 7
        )
    )
)

What I'm doing here is creating a scrollable gallery of 12 weeks from the week beginning set by the locStartDate variable. For each week I then look up the BookingList to find the record for the relevant week if it exists. That way I can display all 12 weeks in the range, even if there is no booking against them.

The reason for the two parts to the WeekBeginning lookup is that SharePoint won't allow selection of date columns using "=" on its own from PowerApps (that's a whole topic for another time), so I've had to put ">=" and "<=" to achieve that effect.

Helpful resources

Announcements
Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Join THE global Microsoft Power Platform event series

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Microsoft Business Applications Virtual Launch

Join us for the Microsoft Business Applications Virtual Launch Event on Thursday, April 2, 2020, at 8:00 AM PST.

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (8,595)