cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AndyDickson
Frequent Visitor

Clear Collection once Patch successful

Hi,

I am fairly new to powerapps and am trying to build an App which holds records in a local collection, patches them to a SharePoint list then clears the list once the Patch has been executed successfully. I am happy with the Patch syntax however I cant work out how to initiate any follow up behavior once the patch has been successful, ie clear collection, post error message etc.

I have pasted my Patch below wich is currently triggered by pressing a button although I aim to build this into the workflow later once I understand the syntax required for this: ('LocalVisits' is the collection and 'PPM Visit Records' is the Sharepoint list with matching fields)

ForAll(

    LocalVisits,

    Patch(

        'PPM Visit Records',

        Defaults('PPM Visit Records'),

        {

            Title: Title,

            Start_Time: Start_Time,

            Visit_Region: Visit_Region,

            Visit_Location: Visit_Location,

            Team_Size: Team_Size,

            Activity: Activity,

            Comments: Comments,

            Latitude: Latitude,

            Longitude: Longitude

        }

    )

)

 

Any assistance would be greatly appreciated.

4 REPLIES 4
pontusofsweden
Resolver IV
Resolver IV

Hi!

 

To clear the collection after the patch operation, just add the Clear function after Patch. See my addition below in red:

 

ForAll(

LocalVisits,

Patch(

'PPM Visit Records',

Defaults('PPM Visit Records'),

{

Title: Title,

Start_Time: Start_Time,

Visit_Region: Visit_Region,

Visit_Location: Visit_Location,

Team_Size: Team_Size,

Activity: Activity,

Comments: Comments,

Latitude: Latitude,

Longitude: Longitude

}

)

;Clear(LocalVisits)

 

BR

Pontus

Many thanks for the response.

 

Will this only clear the collection if the patch is successful or will it clear it regardless? My concern is that if for some reason the patch is not successful the records will be lost with no warning.

 

Thanks

Andy 

Hi,

 

No this removes the records regardless.

 

However, since the Patch function returns the patched record, you could use a Collect function (new collection) to fetch the successful records and the use it to remove recors from LocalVisits. So, it´s a 2 step rocket:

 

1. 

ForAll(

LocalVisits,

Collect(RemoveRecords,

Patch(

'PPM Visit Records',

Defaults('PPM Visit Records'),

{

Title: Title,

Start_Time: Start_Time,

Visit_Region: Visit_Region,

Visit_Location: Visit_Location,

Team_Size: Team_Size,

Activity: Activity,

Comments: Comments,

Latitude: Latitude,

Longitude: Longitude

}

)

)

)

 

2. Remove if success. Dont know what the unique identifier is here. Lets pretend that Title is unique:

Remove(LocalVisits, Filter(LocalVisits, Title in RemoveRecords.Title))

 

 

BR

Pontus

Many Thanks for your assistance with this and apologies for the delayed response - My only concern with the two stage approach involving collecting the posted records back is whether this would present some issues as the dataset in SharePoint grows into the thousands which I am expecting it to over time.

 

I found an old thread which suggested using "If(IsEmpty" syntax to return a notification when Patch performed successfully. This appears to work well and I have experimented with changing the notification function to clear and it works. I just couldn't work out how to fit my Patch into the syntax as opposed to simply posting the "PowerApps Test" in the title of the SharePoint list as shown below.

 

If(

    IsEmpty(

        Errors('PPM Visit Records',Patch('PPM Visit Records',Defaults('PPM Visit Records'),{Title:"PowerApps Test"}))

    ),

    Notify("Patch Successfully",NotificationType.Success),

    Notify("There is an error with your Patch function",NotificationType.Error)

)

Helpful resources

Announcements
Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on August 17, 2022 at 8am PDT.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Users online (4,704)