Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

ComboBox Updates Causing Data Overwrite in PowerApps Gallery?

(0) ShareShare
ReportReport
Posted on by 55

Hey all, beating my head against the wall trying to fix this issue with our charitable product request app and thought I'd throw a Hail Mary and hope someone might see something I'm missing or have a fix! It's a bit complicated so sorry for the long post, but there's probably an easier way to do this or something I'm not doing right.

It should be pretty simple:

1) They fill out a form;

2) They add products to this form (it uses a ComboBox to lookup the codes and fill in the item name, quanity, and warehouse);

3) The data is managed in a collection/gallery for viewing and for our patch to Sharepoint.

If we just add the items and fill it in and submit, everything is good. But if they have 5 products added for example, then change one of them, the other boxes seem to randomly change and duplicate or even change to a separate code somehow -- this GIF is a bit long too but will show how sometimes it's fine, other times it randomly changes?

 

prod-request-issues.gif

 

Objective

The goal of our PowerApps screen is to create a "New Product Request" form that allows users to add multiple products to a request, fill out necessary details, and submit the form. Each product is represented as a row in a gallery, and users can update specific details for each product. The final data is submitted to a SharePoint list.

Key Functionalities

  1. Initialization of Data:

    • On screen load, we initialize the ProductCollection and other necessary variables. Each product in the collection is assigned a unique ItemID to distinguish it from others.
  2. Adding New Products:

    • When the "Add Products" button is clicked, a new row (product) is added to the ProductCollection with default values. This new product is assigned a unique ItemID.
  3. Updating Product Details:

    • Users can update the ItemCode using a ComboBox (ComboBoxItemCode_1). This ComboBox auto-fills other fields such as ItemName, ProductType, and WarehouseCode. These fields cannot be directly edited by the user and are updated automatically based on the selected ItemCode.
    • Users can manually update the CaseQuantity.
  4. Submitting the Form:

    • On form submission, the data from ProductCollection is combined with other form data and submitted to a SharePoint list. 

Detailed Code Explanation

1. Initialization

When the screen loads, we clear any existing data in the collection and reset the main form. We also initialize a request number and set up the ProductCollection with a default item. The initialization code is as follows:

 

// OnVisible property of the screen
Clear(ProductCollection);
ResetForm(Form4);
Set(RequestCounter, RequestCounter + 1);
Set(RequestNumber, "PR" & Text(RequestCounter, "000"));
Set(NextItemID, 1);

Collect(ProductCollection, {
 ItemID: NextItemID,
 RequestNumber: RequestNumber,
 ProductType: "",
 ItemName: "",
 ItemCode: "",
 WarehouseCode: "",
 CaseQuantity: 0
});

 

 

 

Purpose:

  • This code initializes the ProductCollection and sets up a new request number. A default product item is added to the collection with an ItemID starting from 1.

2. Adding New Products

When the "Add Products" button is clicked, the item number is incremented, and a new product row is added to the ProductCollection with default values. The code for adding new products is:

 

 

// OnSelect property of AddProductsBTN
Notify("Please Add Products.", NotificationType.Information);

Set(NextItemID, NextItemID + 1);

Collect(ProductCollection, {
 ItemID: NextItemID,
 RequestNumber: RequestNumber,
 ProductType: "",
 ItemName: "",
 ItemCode: "",
 WarehouseCode: "",
 CaseQuantity: 0
});

Notify("New product row added to collection.", NotificationType.Success);

 

 

Purpose:

  • This code runs when the "Add Products" button is clicked. It increments the item number and adds a new product row to the ProductCollection with default values.

Updating Product Details

Users can update product details using various input controls within the gallery. For instance, when the ComboBoxItemCode_1 changes, the product details are updated in the collection. The code for updating product details is:

 

 

// OnChange property of ComboBoxItemCode_1
Patch(
 ProductCollection,
 LookUp(ProductCollection, ItemID = ThisItem.ItemID),
 {
 ItemCode: Self.Selected.Title,
 ItemName: LookUp(dstOffering, Title = Self.Selected.Title, 'Item Name'),
 ProductType: LookUp(dstOffering, Title = Self.Selected.Title, 'Dp Type'),
 WarehouseCode: LookUp(dstOffering, Title = Self.Selected.Title, 'Warehouse Code')
 }
);

Notify("Product details updated.", NotificationType.Information);

 

 

Purpose:

  • This code runs when the selected item in the ComboBoxItemCode_1 changes. It updates the relevant product row in ProductCollection with the new product details.

When the CaseQuantityInput_1 changes, the quantity is updated:

 

 

// OnChange property of CaseQuantityInput_1
Patch(
 ProductCollection,
 LookUp(ProductCollection, ItemID = ThisItem.ItemID),
 { CaseQuantity: Value(Self.Text) }
);

Notify("Case Quantity updated.", NotificationType.Information);

 

 

Purpose:

  • This code runs when the quantity input changes. It updates the relevant product row in ProductCollection with the new quantity.

Controls in the Gallery:

  1. ComboBoxItemCode_1:

    • Purpose: Allows users to select an item code, which auto-fills other fields (ItemName, ProductType, WarehouseCode).
    • Properties:
      • DefaultSelectedItems: LookUp(dstOffering, Title = ThisItem.ItemCode)
      • OnChange: Updates the relevant product row in ProductCollection.
  2. ItemNameInput_1 (Read-Only):

    • Purpose: Displays the item name based on the selected item code.
    • Properties:
      • Default: ThisItem.ItemName
  3. ProductTypeInput_1 (Read-Only):

    • Purpose: Displays the product type based on the selected item code.
    • Properties:
      • Default: ThisItem.ProductType
  4. WarehouseCodeInput_1 (Read-Only):

    • Purpose: Displays the warehouse code based on the selected item code.
    • Properties:
      • Default: ThisItem.WarehouseCode
  5. CaseQuantityInput_1:

    • Purpose: Allows users to enter the quantity of the product.
    • Properties:
      • Default: ThisItem.CaseQuantity
      • OnChange: Updates the relevant product row in ProductCollection.

Methods Tried

  1. Using CountRows:

    • Initially used CountRows to determine the next item number but faced performance issues and data inconsistencies.
  2. Using GUIDs:

    • Considered using GUID() for unique identification but added complexity without resolving the core issue.
  3. Using Global Variables:

    • Implemented global variables like NextItemID to manage unique item identification.
  4. ForAll:

    • Attempted using ForAll to iterate through the collection for updates but faced performance concerns and still encountered the issue.

Summary

  • Objective: Create a form to add multiple products, update details, and submit them.
  • Problem: When modifying a product's details using a ComboBox, changes are unexpectedly affecting other items in the gallery.



Categories:
  • KBpwr Profile Picture
    55 on at
    Re: ComboBox Updates Causing Data Overwrite in PowerApps Gallery?

    Jay2814, that fixed it! Thanks so much for reading through my long post and helping with the issue. Now I just wish I posted earlier so I could better understand how a gallery actually works and why my method was wrong.

    Appreciate your help and for explaining the issue to me. 

  • Verified answer
    Jay2814 Profile Picture
    33 on at
    Re: ComboBox Updates Causing Data Overwrite in PowerApps Gallery?

    Hi KBpwr, 

     

    It might be because of your OnChange property for ComboBoxItemCode_1. Since galleries duplicate the same element for all items in the gallery, it is creating a conflict and is applying the patch for the other products.

     

    For example, you may be intending to only patch product ID 1 but since the element ComboBoxItemCode_1 is technically the same for all of the other products, it is also patching products ID 2 and 3 with the changes from product ID 1.

     

    Possible Solution:

    On the On Select property for ComboBoxItemCode_1 you can create some sort of variable to track the actual ID of the product you are intending on modifying:

    //On Select
    Set(parentID, ThisItem.ID);

     Then in your On Change property, slightly modify your code as the following to only patch that specific record:

    // OnChange property of ComboBoxItemCode_1
    If(parentID=ThisItem.ID), Patch(
     ProductCollection,
     LookUp(ProductCollection, ItemID = ThisItem.ItemID),
     {
     ItemCode: Self.Selected.Title,
     ItemName: LookUp(dstOffering, Title = Self.Selected.Title, 'Item Name'),
     ProductType: LookUp(dstOffering, Title = Self.Selected.Title, 'Dp Type'),
     WarehouseCode: LookUp(dstOffering, Title = Self.Selected.Title, 'Warehouse Code')
     }
    ));
    
    Notify("Product details updated.", NotificationType.Information);

     

    Let me know how this works out for you.

     

    Thank You.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Building Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 110 Most Valuable Professional

#2
MS.Ragavendar Profile Picture

MS.Ragavendar 77

#3
stampcoin Profile Picture

stampcoin 52

Overall leaderboard