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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patching a Collection ...
Power Apps
Unanswered

Patching a Collection from a Gallery

(0) ShareShare
ReportReport
Posted on by 439
Hi all,
 
I am going round in circles on this and have tried multiple things to resolve it (patch the same collection, patch a different collection, RenameColumns, ThisRecord, ThisItem, tried a version of this: JOIN One Columns From Another Collection - Matthew Devaney, etc). At the point now where I'm hoping someone from the community can help.
 
I have a collection, col1, which populates a gallery gal1.
Some Items in the Gallery will have a DropDown control drp1 (this is dependant on an attribute stored in col1). The Dropdown contains two values, 1 and 2.
 
OnSelect of a button outside of the Gallery, I want each item in the Gallery to be checked, and when the Dropdown control is applicable to an item, I want to patch the value of 1 or 2 (as selected in the Dropdown) to a column in the collection, OptValue.
 
I think that it is not possible to patch the collection that supplies the Gallery directly (col1) (fairly sure nothing happens when that is attempted) so I have set up col1a in the Patch arguments. col1a is a copy of col1, but with the target column OptValue with a value of 0 for each item.
 
No matter what I try to do, the values are being patched correctly but only to the first record in the collection. I can't seem to LookUp the correct record in the collection despite that record having a unique identifier, OPID.
 
Can anyone assist with this? 
 
Here is the code as it stands:
 
If(
        'drp1'.Selected.ID = 1,
        // true branch
        Patch(col1a, LookUp(col1a, OPID = ThisRecord.OPID),
        {OptValue: 1}), 
        // false branch
        Patch(col1a, LookUp(col1a, OPID = ThisRecord.OPID),
        {OptValue: 2}
        )
        );
 
Looking at it now, I don't think I need the If statement as the Value of the Dropdown is all that needs to be patched. But the same problem will still exist - it refuses to LookUp the correct record in the Collection, using the OPID (value; unique), and the value just goes in to the first record in the collection.
 
Thanks all for the help.
Categories:
I have the same question (0)
  • gregmacg Profile Picture
    on at
    I don't think I can understand your issue 100%, but I will say that I sometimes find myself using text labels inside a gallery, assigning a value to those labels that I'll end up using elsewhere, and then hiding the labels so they're not visible to the user.
     
    I can then recall the value of that hidden text label and use it in a ForAll outside the gallery. Have you considered storing the value of OPID in a text label?
     
    Not sure if helpful, but it could get you thinking in a new direction ;)
  • pp365 Profile Picture
    439 on at
    Hi gregmacg,
     
    Many thanks for your reply and suggestion. The problem is, the data it needs to match against is already there within the record in the Gallery (the unique value of OPID). I'm trying to avoid having to duplicate this data elsewhere just to allow Power Apps to Lookup the relevant record so I'd like to avoid having data copied in to a label, or duplicating the entire Collection, etc. if I can avoid this.
     
    What I can't understand is why the collection that provides data to the Gallery (col1), including the value ID column of OPID, cannot be used to look up a numeric value in the collection I'm trying to Patch, col1a. To my understanding this is attempting to use two numerical values and match these, e.g. 1 = 1, 2 = 2, etc. It will just not work for some reason.
     
    I've tried loads of different things here, I've tried to avoid the Lookup altogether as it's already within a For All, so I've tried This Record, This Item, etc. but everything either throws an error, or, I get this odd behaviour which I don't think I've ever seen before, whereby the values are always patched in to the first record of the Collection no matter what.
     
    Thanks again!
  • Sam_Fawzi Profile Picture
    750 Super User 2025 Season 2 on at
    Hey,
     
    You Can try using ForAll Function, 
     
    Here is a sample code structure

    ForAll(
        col1,
        Patch(
            col1a,
            LookUp(col1a, OPID = OPID),
            {
                OptValue: If(
                    'drp1'.Selected.ID = 1,
                    1,  // If dropdown value is 1
                    2   // If dropdown value is not 1 (assumed to be 2 based on the description)
                )
            }
        )
    )
     
     





    Hope this Help!
    Cheers,
    Sam
  • pp365 Profile Picture
    439 on at
    Hi Sam,
     
    Many thanks for your reply. The code works to complete a but results in exactly the same problem I've been experiencing: it is not correctly looking up on the OPID value and is just patching to the first record in the collection no matter what.
     
    For example, I've just completed a test with this code where I have two records, the OPID value is 7 for one and 9 for the other. The record to which the value from the dropdown should be patch is the record with the OPID value of 9, however, it has patched to the value of 7 for reasons I cannot see.
     
    I've probably wasted something in the region of about 5 hours on this issue now... if anyone could advise further it would be much appreciated, many thanks!
  • Suggested answer
    realwillwright Profile Picture
    772 Moderator on at
    Hi,
     
    Something like this should work. You get all the data from the gallery and store it in a collection (you may need to add OPID), then write it to the collection.
     
    ClearCollect(colGalData, gal1.AllItems);
    ForAll(colGalData,
        UpdateIf(col1, OPID = colGalData[@OPID],
        {
            OptValue: drp1.Selected.Value
        }
    )
    
    Cheers
  • pp365 Profile Picture
    439 on at
    Hi realwillwright,
     
    Thank you for replying, I've tried that, I am now getting no values written for OptValue at all. Previously I was getting the correct value, but in the wrong record of the collection.
     
    OPID is included in the collection that the Gallery is sourced from. So in your code it is in colGalData when this is collected from the Gallery. It is also in col1. But yet it is still refusing to look up against each other and I don't know why!
     
    You said "(you may need to add OPID)". Please could you expand on what you mean so I can try this? If you can include it in the code that would be very helpful. 
     
    Thank you again.
  • pp365 Profile Picture
    439 on at
    Hi again everyone,
     
    In case this is helpful about the underlying issue. In the LookUp, if I replace the 'Dynamic' part of the lookup with a fixed value, the patch is working.
     
    For example,  taking Sam's solution, if I replace the second part of the OPID argument with the fixed value of '7' which matches the OPID value in the second record of two records in the target collection, I am getting the correct patch outcome (see example code below).
     
    LookUp(col1a, OPID = 7),
     
    This seems to suggest the LookUp is not working in a dynamic way, but I have no idea why it is not working.
     
     
    ForAll(
        col1,
        Patch(
            col1a,
            LookUp(col1a, OPID = 7),
            {
                OptValue: If(
                    'drp1'.Selected.ID = 1,
                    1,  // If dropdown value is 1
                    2   // If dropdown value is not 1 (assumed to be 2 based on the description)
                )
            }
        )
    )
     
  • pp365 Profile Picture
    439 on at
    Hi everyone,
     
    think I've got a solution to this issue which is based on @realwillright 's solution proposed below. The update I had to make was to use a collection (col1a) that was different to the collection (col1) that was suppling the Records to the Gallery, gal1. (If col1 was attempted in the 'UpdateIf', nothing is patched to the collection).
     
    The solution code:
    (Note the use of col1a in the 'UpdateIf', not col1).
     
    ClearCollect(col1a, col1);

    ClearCollect(colGalData, 'gal1'.AllItems);
    ForAll(colGalData,
        UpdateIf(col1a, OPID = colGalData[@OPID],
        {
            OptValue: drp1.Selected.Value
        }
      )
    )
     
    Result:
    The appropriate record in collection col1a is identified (by looking up the unique identifier in column OPID) and is patched in column 'OptValue' with the Value of the Record selected in dropdown drp1,
     
    I will be testing this further today/tomorrow and will report back on this thread to fully confirm it's working as required. 
     
    A note of thanks:
    I would like to say a huge thank you to all those who have responded to this thread and suggested solutions. Without this Community and those helping others within it, issues like this are extremely difficult/impossible to solve, and involve a huge amount of unnecessary additional resource wastage through guesswork/trial and error testing etc. once routine methods of resolution have been exhausted.
     
    Many thanks again to all for the help and especially @realwillright for the solution.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard