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 

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), {....

_____________________________________________________________________________________
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  ok i change this but still when i edit the master record and i save the form the child record will be recreated, instead of been ignored since it is already inside the data source.. so again when i am patching the collection to sharepoint i need a way to only patch if the collection does not include the item Id is this possible?

 

Mainly inside this code:-

Patch12.png

RandyHayes
Super User
Super User

@johnjohn123 

Yes, you are stating that you want to Update existing and Create new.  So if you are wanting to update existing, then I need to know the formula you are using to populate the collection with the existing items.  You have provided me with the formula you use to add a new item to the collection (which is referencing the wrong Defaults datasource), but not for populating with existing.

What is that?

_____________________________________________________________________________________
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  sorry if i were not clear.. there is no Edit.. only you can create new child or remove child no edit at all. once a child record is added to a master record you can only remove it

Thanks

@RandyHayes  i tried this code,, and seems it will add and ignore correctly... so do you have any comment on it:-

 

p2.png

 

so i will only patch the item inside the collections if the item ID is empty or does not exists inside the collection.. any comment on my formula?

thanks

RandyHayes
Super User
Super User

@johnjohn123 

How are you recording that an item is removed?  This is something that needs to be done separately. 

 

As for the formula, avoiding If statements is best...

Collect(SNMItems,
    ForAll(Filter(colSItems, IsBlank(ID)) As _item,
      {'Vendor Price': _item.'Vendor Price',
       Vendor: _item.Vendor,
       'Vendor Part No.': _item.'Vendor Part Number',
       SID: Self.LastSubmit.ID
      }
   )
)

Note for above, I still recommend my changes to your OnSelect of the Save Icon to resolve the LastSubmit.ID issue.

And, not sure what the CountRows purpose was...

 

Now, removing items is a different story, and I'd need to know how you are identifying removed items.

_____________________________________________________________________________________
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  the CountRows is to check if the Id of the item already exists inside the collection

RandyHayes
Super User
Super User

@johnjohn123 

It's not needed.  Your check for blank is all you need.  There will never be an existing ID because it doesn't exist.  You're only concerned with adding new records...blank ID's

_____________________________________________________________________________________
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  but if i remove the CountRows check and i edit a master record and save it.. then duplicate child will be created!!

RandyHayes
Super User
Super User

@johnjohn123

If the ID is blank, then it should be created. The filter will ONLY produce records with no ID. Those are records to be created.

You still haven't shared the formula used to populate the collection with existing records and your add new record formula was incorrect.

 

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

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,811)