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
}
)
);
Solved! Go to Solution.
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.
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.
Thank you @RandyHayes ! I frequently forget about the WITH function and your code is a lot cleaner and easier to follow/update.
User | Count |
---|---|
261 | |
125 | |
99 | |
48 | |
45 |