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

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
Highlighted
Super User III
Super User III

@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. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!
Highlighted

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😂

Highlighted

@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. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!
Highlighted

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

Highlighted

@markslosberg 

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

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (8,074)