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

Collections: Update if exists or add if new

I have a collection shopping cart (collectCart) that contains items, quantities, and price.  My goal is to add new items to the collection if new, or increment the quantity if existing.  While my code works, I'm trying to make it more efficient.  Any advice is appreciated!

If(
    IsBlank(
        LookUp(
            collectCart,
            CATALOG_ID = ThisItem.CATALOG_ID
        )
    ),
    Collect(
        collectCart, 
        {
            CATALOG_ID: ThisItem.CATALOG_ID,
            QUANTITY: 1,
            QUOTED_PRICE: ThisItem.PRICE
        }
    ),
    Patch(
        collectCart,
        LookUp(
            collectCart,
            CATALOG_ID = ThisItem.CATALOG_ID
        ),
        {
            QUANTITY:  LookUp(collectCart,CATALOG_ID = ThisItem.CATALOG_ID,QUANTITY) + 1
        }
    )
);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@JimSutt1 

Not much to make it more efficient when dealing with collections as they do not have primary keys.  Ultimately, the Patch function will do all the work for you (create or update), but that implies that it has a primary key to work from.  So the only changes I would suggest would be this:

With({_record: LookUp(collectCart, CATALOG_ID = ThisItem.CATALOG_ID)},

    If(IsBlank(_record.CATALOG_ID),
        Collect(collectCart, 
            {
               CATALOG_ID: ThisItem.CATALOG_ID,
               QUANTITY: 1,
               QUOTED_PRICE: ThisItem.PRICE
            }
        ),
        Patch(collectCart, _record,
            {QUANTITY:  _record.QUANTITY + 1}
        )
    )
)

 

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!

View solution in original post

2 REPLIES 2
RandyHayes
Super User
Super User

@JimSutt1 

Not much to make it more efficient when dealing with collections as they do not have primary keys.  Ultimately, the Patch function will do all the work for you (create or update), but that implies that it has a primary key to work from.  So the only changes I would suggest would be this:

With({_record: LookUp(collectCart, CATALOG_ID = ThisItem.CATALOG_ID)},

    If(IsBlank(_record.CATALOG_ID),
        Collect(collectCart, 
            {
               CATALOG_ID: ThisItem.CATALOG_ID,
               QUANTITY: 1,
               QUOTED_PRICE: ThisItem.PRICE
            }
        ),
        Patch(collectCart, _record,
            {QUANTITY:  _record.QUANTITY + 1}
        )
    )
)

 

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!

Thank you @RandyHayes !  I frequently forget about the WITH function and your code is a lot cleaner and easier to follow/update.  

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (4,970)