cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
johnjohn123
Super User
Super User

check if the Collection already contain and item before patching it to prevent saving it twice

I have the following code, to Loop on all the items inside a collection named "colSItem" and patch them to a SharePoint list named @"SNMItems":-

 

patchthemto.png

but i want to only patch the items that are new to the collection... so how i can define to only patch the item is its id does not exists inside the collection?

21 REPLIES 21
RandyHayes
Super User
Super User

@johnjohn123 

Your collection should always have the same schema as the records that you wish to apply them to - or at least as close a possible.

So, if this is a collection of SharePoint records, then the ID will either have a value (existing) or be Blank() (new).

With that combination, using a formula such as this:

   Path(yourDataSource, yourCollection) 

will create new records and update existing records.

Note: the above formula implies and exact match to the records.

If the records do not match, then formulas like this will be equivalent:

Patch(yourDataSource,
    ForAll(yourCollection,
       {ID: ID,
        someColumn: someCollectionColumn
       }
    )
)

        

 

I hope this is helpful for you.

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

@RandyHayessorrt i did not get your reply.. where you are checking if the item Id already exists inside the collection?

RandyHayes
Super User
Super User

@johnjohn123 

Sorry, the formula I provided would create new records and update existing.

If you are only concerned with the New items, then filter your collection by blank ID's.

And if you're creating new items only, then you can use the Collect function.

ex.

    Collect(yourDataSource, Filter(yourCollection, IsBlank(ID)))

 

Or for the other:

Collect(yourDataSource,
    ForAll(Filter(yourCollection, IsBlank(ID)),
       {ID: ID,
        someColumn: someCollectionColumn
       }
    )
)

 

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

@RandyHayes  now the formula i need should work for new items + updating items.. for example when i create a new master record and i enter new child records.. on other scenarios i might edit the master record and do not add any child.. in the later approach i do not want to have duplicate items inside the child.. did you get my point?

@RandyHayes  i tried your formula and still duplicate child record will be created incase i update the master record! any advice please?

RandyHayes
Super User
Super User

@johnjohn123 

Can you provide the formula you are now using as well as the formula you use to populate the collection you are using?

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

To Populate the collection with a new item i am using:-

 

onselect.png

 

and to Patch the collection to SharePoint  i am using:-

 

Patch12.png

 

Thanks

 

RandyHayes
Super User
Super User

@johnjohn123 

You need to change the Defaults function to specify the DataSource name - not the collection name.

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

@RandyHayes  can you describe your point in more details? now the code to add a new Item to the collection is working well.. this code, is adding a new item to the collection without any issues

johnjohn123_0-1629399635808.png

 

Helpful resources

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

Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on June 15, 2022 at 8am PDT.

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.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

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