cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
markslosberg
Resolver II
Resolver II

Using a ForAll function to Patch multiple records in a CDS entity

I am trying to decipher some strange behavior with the ForAll function.

 

I have a selection of records from a CDS entity (in this case a group of records that all start with a particular string "OS").  I have filled a gallery with these records.

Locations starting with OS.jpgthis Gallery is filled using

 

 

 

 

Filter(
    'Sales Locations',
    StartsWith(
        SalesLocationID,
        "OS"
    )
)

 

 

 

 

The Change Selected Record button works perfectly

 

 

 

 

Patch(
    'Sales Locations',
    LookUp(
        'Sales Locations',
        SalesLocationID = galSaleLocationsstartingwithOS.Selected.SalesLocationID
    ),
    {
        'Sales Person ID Import': "80031",
        'Sales Person ID': LookUp(
            'Sales People',
            SalepersonID = "80031"
        )
    }
);

 

 

 

 

There are a lot of these records so when I try to use this Patch command inside of a ForAll function (properly modified I think to reflect the different syntax using ThisRecord instead of the gallery.selected) it isn't parsing or operating correctly:

 

 

 

 

ForAll(galSaleLocationsstartingwithOS.AllItems,
    Patch(
    'Sales Locations',
    LookUp(
        'Sales Locations',
        SalesLocationID = ThisRecord.SalesLocationID
    ),
    {
        'Sales Person ID Import': "80031",
        'Sales Person ID': LookUp(
            'Sales People',
            SalepersonID = "80031"
        )
    }
))

 

 

 

 

I got frustrated and for this instance, I just brute-forced the change manually but I am going to want to do these kinds of bulk updates in the future and would like to figure out what I am doing wrong of if I am encountering an undocumented "feature"😂.

 

here is the ForAll and ThisRecord documentation (@GregLindhorst maybe @GregLi ) I am using to guide me.  I am also going to look at doing this inside of a Flow too but this seemed so much more straightforward (stupid me😂

 

 

5 REPLIES 5
RandyHayes
Super User
Super User

@markslosberg 

Please take a look at this post that I put out on the use of ThisRecord in a ForAll.  I believe it will be helpful for you.

 

I do hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

It was helpful.  I downloaded your example, studied and ran it.

 

I was able to use the format to run in my own environment using the UpdateIf statement you used but I have not yet been able to use it to drive a Patch statement which I am more familiar with and was planning on using in this case.  I haven't had the time to do the couple of hours of testing required to find out if it will ever work with a Patch command or just with the UpdateIf (and all the learning I will have to do to master that function, which will do me good).

 

I do tend to agree with your diagnosis that this might be a "documentation problem" rather than a "technical problem" but who would be surprised by that Power App reality😂

@markslosberg 

The only real difference with Patch over UpdateIf is that UpdateIf is faster and more precise.  Patch requires two data operations - one to lookup the record and one to update it.  UpdateIf does it in one shot.

The only time Patch has to be used is if you are creating a new record (as you will need the Defaults values).

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thank you. Very informative although now I have to go back and modify a bunch of existing code. 😂

@markslosberg 

Well...if it ain't broke!!!   I'd leave and adjust for the future as needed. 😉

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (3,894)