web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / check if the Collectio...
Power Apps
Unanswered

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

(0) ShareShare
ReportReport
Posted on by 3,514

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?

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

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

  • johnjohn123 Profile Picture
    3,514 on at

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

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @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
     }
     )
    )
    

     

  • johnjohn123 Profile Picture
    3,514 on at

    @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?

  • johnjohn123 Profile Picture
    3,514 on at

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

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @johnjohn123 

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

  • johnjohn123 Profile Picture
    3,514 on at

    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 Profile Picture
    76,299 Super User 2024 Season 1 on at

    @johnjohn123 

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

  • johnjohn123 Profile Picture
    3,514 on at

    @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

     

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @johnjohn123 

    Yes, but you are patching the Defaults and the hand-written record to your collection.  Defaults is returning the default record of the *collection* not your datasource.  Therefore the collection will NOT have the schema of the datasource record, but of the collection - which has no schema.

    You need to reference the Datasource in Defaults, otherwise your collection will not have an ID column in it.

    Patch(colSItems, Defaults(yourDataSource), {....

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 414

#2
WarrenBelz Profile Picture

WarrenBelz 377 Most Valuable Professional

#3
Kalathiya Profile Picture

Kalathiya 315 Super User 2026 Season 1

Last 30 days Overall leaderboard