Hi guys,
So i'm trying to work with collections so i can make an offline app. What i am trying to do is to check if a collection item already exists. If it does the quantity needs to be updated. If it doesn't exist it needs to be added to the collection.
Currently i make my collection like this:
Collect(
matList;
{
material: varThisMaterial;
quantity: varAmount;
bonitem: varBonItems;
bonNr: varBonNr
}
)
I would need like a function that does this: if( matlist.material=varThisMaterial && matlist.bonitem=varBonItems && bonNr=varBonNr, update quantity to quantity+varamount, else make a new collection item.
Underneath picture will make u understand what i am trying to do:
As you can see on the picture i have the same items a few times in my collection. I just want them to add up whenever i add more of them.
Greetings Anthony
Try this, it's a much cleaner solution and eliminates the need for the extra variables. If you still get errors with varAmount, you might have to wrap it like this: Value(varAmount)
If(
CountIf(matlist, Title = varThisMaterial) >0,
With(
{_item: LookUp(matlist, Title = varThisMaterial && bonitem = varBonItems && bonNr = varBonNr)
},
UpdateIf(matlist, !IsBlank(_item.Title) && _item.Title = varThisMaterial, {quantity: _item.quantity + varAmount})
),
Collect(matlist,
{Title: varThisMaterial,
quantity: varAmount,
bonitem: varBonItems,
bonnr: varBonNr
}
)
)
I fixed it and all works fine now. I had to change the var (varAmount + quantity) to a new var "varPatchAmount". Now the code runs perfect and does the job! Thank you very much for your great help!
Here's the code:
If(
CountIf(matlist, Title = varThisMaterial && bonitem = varBonItems && bonnr = varBonNr) >0,
Set(varRecord,
First(
Filter(matlist,
Title in varThisMaterial && bonitem in varBonItems && bonnr in varBonNr).quantity
)
); Set(varPatchAmount,varRecord.quantity + varAmount);
Patch(matlist,
First(
Filter(matlist,
Title in varThisMaterial && bonitem in varBonItems && bonnr in varBonNr)),
{
quantity: varPatchAmount
}
),
Collect(
matlist,
{
Title: varThisMaterial,
quantity: varAmount,
bonitem: varBonItems,
bonnr: varBonNr
}
)
)
I tried your code but it makes the var "varAmount" give a lot of errors. It also makes the UI to insert varAmount completely full of errors. Also i changed the collection items names a little because i tried something else with my sharepointlist (that didn't work aswell).
varAmount is the "text" property of a label. I made a UI for the user to insert the amount they want (did this with string manipulation):
My onstart:
Collect(
matlist,
{
Title: "",
quantity: "0",
bonitem: "",
bonnr: ""
}
);
Clear(matlist
My onselect checkicon:
If(
CountIf(matlist, Title = varThisMaterial && bonitem = varBonItems && bonnr = varBonNr) >0,
Set(varRecord,
First(
Filter(matlist,
Title in varThisMaterial && bonitem in varBonItems && bonnr in varBonNr).quantity
)
); Set(varAmount,varRecord.quantity + varAmount);
Patch(matlist,
First(
Filter(matlist,
Title in varThisMaterial && bonitem in varBonItems && bonnr in varBonNr)),
{
quantity: varAmount
}
),
Collect(
matlist,
{
Title: varThisMaterial,
quantity: varAmount,
bonitem: varBonItems,
bonnr: varBonNr
}
)
)
errors i get:
I tried changing a lot and nothing worked..
You can create the collection on app start like this:
Collect(matlist,
{
material:"",
quantity:0,
bonitem:"",
bonNr:""
}
); Clear(matlist)
Then change the formula I gave you previously to this:
If(
CountIf(matlist, material = varMaterial && bonitem = varBonItems && bonNr = varBonNr) >0,
Set(varRecord,
First(
Filter(matlist,
material in varMaterial && bonitem in varBonItems && bonNr in varBonNr).quantity
)
); Set(varAmount, varRecord.quantity + varAmount);
Patch(matlist,
First(
Filter(matlist,
material in varMaterial && bonitem in varBonItems && bonNr in varBonNr)),
{
quantity: varAmount
}
),
Collect(matlist,
{
material: varMaterial,
quantity: varAmount,
bonitem: varBonItems,
bonNr: varBonNr
}
)
)
How should i change the code so that when i start the app and the collection doesn't exist yet, i don't get errors?
Here's the full code (it is the "on select" property of a check icon):
Can you provide a screenshot of the entire code with the error lines? Also to test the code, you might put a button on the screen to create your collection. You will definitely get errors if the collection doesn't exist.
dear @JR-BejeweledOne ,
I tried your code but i seem to be getting some errors. Is this maybe because the first time i'm trying to add materials the collection doesn't exists yet and because of that it gives errors?
This is the error i get:
Boolean expected & invalid arguments in filter.
This is the code that gives errors (i need to use ";" somehow in my powerapps instead of ","):
If(
CountIf(matlist; material = varThisMaterial && bonitem = varBonItems && bonNr = varBonNr) >0;
Patch(matlist;
First(
Filter(matlist;
material in varThisMaterial && bonitem = varBonItems && bonNr = varBonNr;
{
quantity: quantity + varAmount
}
)
)
);
Collect(
matlist;
{
material: varThisMaterial;
quantity: varAmount;
bonitem: varBonItems;
bonNr: varBonNr
}
)
)
Thank you for helping me!
I think you are looking for something like this:
If(
CountIf(matlist, Material = varMaterial && bonitem = varBonItems && bonNr = varBonNr) >0,
Patch(matlist,
First(
Filter(matlist,
Material in varMaterial && bonitem = varBonItems && bonNr = varBonNr,
{
quantity: quantity + varAmount
}
)
)
),
Collect(
matlist:
{
material: varThisMaterial;
quantity: varAmount;
bonitem: varBonItems;
bonNr: varBonNr
}
)
)
WarrenBelz
637
Most Valuable Professional
stampcoin
570
Super User 2025 Season 2
Power Apps 1919
473