cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AnthonyDB
Resolver I
Resolver I

check if collection item exists, if it does update it, if not make the item

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:

 

2021-03-01 14_22_07-Savic - Opgeslagen (niet gepubliceerd) - Power Apps.png

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

1 ACCEPTED SOLUTION

Accepted Solutions

@JR-BejeweledOne 

 

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
               
             }

       )
)

 

View solution in original post

8 REPLIES 8
JR-BejeweledOne
Super User
Super User

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
             }

       )

)



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

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.

2021-03-02 10_03_15-Savic - Opgeslagen (niet gepubliceerd) - Power Apps.png

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!

 

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.



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

@JR-BejeweledOne 

 

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):

 

2021-03-02 13_52_03-Savic - Opgeslagen (niet gepubliceerd) - Power Apps.png

JR-BejeweledOne
Super User
Super User

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

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

@JR-BejeweledOne  

 

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):

 

2021-03-03 09_32_47-Savic - Saved (Unpublished) - Power Apps.png

 

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:

2021-03-03 09_32_08-Savic - Saved (Unpublished) - Power Apps.png

 

I tried changing a lot and nothing worked..

@JR-BejeweledOne 

 

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
               
             }

       )
)

 

View solution in original post

JR-BejeweledOne
Super User
Super User

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

 

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Top Solution Authors
Top Kudoed Authors
Users online (65,978)