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
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (1,372)