web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Assign each new item i...
Power Apps
Answered

Assign each new item in a galley a unique variable

(0) ShareShare
ReportReport
Posted on by 33

Hi guys, I am attempting to create a shopping cart, however I am trying to make it a little more advanced.

 

n0tOm6nGFc.png

This is an image of the shopping cart screen. Currently when I add a new item to the shopping list my + and - buttons are altering all of the items quantities within the shopping cart because the variable is the same. I was wondering if it would be possible to make it so that every time a new item is added to the gallery it assigns the variable to a unique variable. The example:

UpdateContext({quantity:TextInput4 +1})

This is the code inside of the + and - button (With the subtract being -1 instead of +1), then I call that variable into the textbox. Is it possible to create a loop or something similar that says for each item in the gallery assign the quantity variable to quantity1, quantity2 etc.

 

I realise that it would be easier to just remove the + and - buttons and just allow the user to enter the value but I would like to explore this option if it is possible.

 

Thanks in advance guys!

 

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Seanan 

    You will need to skip the variable in this scenario and have an additional column in your gallery Items table.

    What is your current Items property for the gallery?

  • Seanan Profile Picture
    33 on at

    Hi Randy, thank you for getting back so quickly. In the Items property for my gallery I am calling a collection: ColProductOrder. 

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Seanan 

    So then when you create that collection, add a new column to it called Qty - default of 0

    Then, your label (I believe that is a label you have in the picture) in the Gallery should have a text property of : ThisItem.Qty

    Your Plus icon OnSelect : Update(ColProductOrder, ThisItem, {Qty: ThisItem.Qty + 1})

    And your minus icon:  Update(ColProductOrder, ThisItem, {Qty: ThisItem.Qty - 1})

     

    As a bonus, you might set your minus Icon display mode property to: 

       If(ThisItem.Qty<1, Disabled, Edit)

     

     

  • Verified answer
    Seanan Profile Picture
    33 on at

    I may be doing something wrong but I seem to be running into an error that says: "The Qty column inside of your data source expects a record column and not a number".

     

    The code to create my collection is:

    Collect(ColProductOrder,{Name:ThisItem.Title,Price:ThisItem.Price,Brand:ThisItem.Brand,Colour:ThisItem.Colour,Size:ThisItem.Size,Qty:ThisItem})

    The code in the +/- button is:

    Update(ColProductOrder, ThisItem, {Qty:ThisItem.Qty + 1})

    The collection is used to pull specific columns of data from my SharePoint List.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Seanan 

    In that first formula, you are assigning the full record to a numeric column. 

    That formula is not clear as you are referring to ThisItem in all of the columns to add a record to a collection.  But wouldn't the row you are on in the gallery already have that row in it?  Why are you creating a new row in the collection there?

  • Seanan Profile Picture
    33 on at

    msedge_ZjVbIzM7WY.png

    This is my shopping page (Apologies for no images to make it look nicer). Basically When I select an Image you get a popup (Using updatecontext) to make a button appear and some information that allows you to add that item to the cart. So in order to take the selected item and add it to the cart I created a new collection that will then pull only certain columns that I need to display on my cart screen. I apologise if my code is not the greatest or a little confusing as I am still trying to get to grips with using PowerApps. 

  • v-jefferni Profile Picture
    Microsoft Employee on at

    Hi @Seanan ,

     

    The logic from @RandyHayes is all good. I'm here to help incase he is too busy to get back to this thread in time.

     

    I assume the default quantity of items added to the shopping cart will always be set to 1, then the collection creating formula should be as follows:

    Collect(ColProductOrder,{Name:ThisItem.Title,Price:ThisItem.Price,Brand:ThisItem.Brand,Colour:ThisItem.Colour,Size:ThisItem.Size,Qty: 1})

     

    The Qty column should include the default value you want in the shopping cart. Other formulas will exactly like in @RandyHayes 's reply.

     

    Hope this helps.

     

    Best regards,

    Community Support Team _ Jeffer Ni
    If this post helps, then please consider Accept it as the solution to help the other members find it.

  • Seanan Profile Picture
    33 on at

    Hi @v-jefferni,

     

    I apologise for not getting back to you sooner as I have been away.

     

    You solution worked almost perfect and the plus and minus buttons are now working independently. However, it seems that when I click on the plus button it now removed my "title" and "brand" labels. An example is below.

     

    msedge_86klUKwT7B.png

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    The code I have in my title is 

    ThisItem.Name & " " & ThisItem.Colour.Value

    And for the Brand it is:

    ThisItem.Brand.Value

    Is there any obvious mistake I have made? I copied in the collection creating formula you suggested and also used the formula Randy mentioned for the +/- buttons.

  • Seanan Profile Picture
    33 on at

    I have managed to fix the issue now. I realised I needed to call all of the variables into the Update() formula and that seemed to fix it.

     

    Thank you once again for your help guys.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 379 Most Valuable Professional

#2
11manish Profile Picture

11manish 203 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard