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 / Patch function within ...
Power Apps
Answered

Patch function within a Patch function

(0) ShareShare
ReportReport
Posted on by 427

I was hoping someone could help or shed some light on a situation with nested Patch functions.

 

I have a function carrying out a ForAll function, within which, I basically need to create a "parent" list item for a Sharepoint List Item.

As I cannot create variables inside a ForAll function, I was hoping to leverage the fact that the Patch function (for the "parent" record) returns the created record - I could then use that returned value to set the foreign key of the original item being patched:

 

 

ForAll(UpdateCollection, // A List of ChildList Record ID's that need to be updated
 Patch(ChildList,LookUp(ChildList,ID=UpdateCollection[@ID]),
 {
 Parent_ID: Patch(ParentList,Defaults(ParentList), { Title: "New ParentList Record" } ).ID
 }
 )
);

 

 

The "Inner" patch is working fine - I can confirm that the "parent" record is being created correctly, but the "outer" patch is NOT updaing the "Parent_ID" of the ChildList record.

 

Is there some sort of limitation for nesting Patch functions?

Is it to do with the order in which the patches take place? Perhaps the runtime compiler cannot interrupt a patch function with another patch function and then carry on with the 1st patch function.

Or, if all else fails, can anyone else think of another way to achieve the same result? (keeping in mind that you cannot use Set or UpdateContext within a ForAll function) - The only other method I can think of means looping through the entire UpdateCollection 3 times (1. to create parent records, 2. to patch UpdateCollection with lookup to parent record list, 3. to patch child record list)

 

Many thanks to anyone that can help.

James.

 

P.S. I find it amusing that on the the "PowerApps Community Forum", the code snippet function does not have a language style for "PowerApps Script"! - I mean, I love PowerApps, but you gotta love Microsoft for it's lack of common sense sometimes...

Categories:
I have the same question (0)
  • v-monli-msft Profile Picture
    Microsoft Employee on at

    Hi @james_hathaway ,

     

    This cannot be achieved because of the patch order. You understanding is correct. 

     

    Please try with below formula instead:

    Patch(ParentList,Defaults(ParentList), { Title: "New ParentList Record" } );
    ForAll(UpdateCollection, 
     Patch(ChildList,LookUp(ChildList,ID=UpdateCollection[@ID]),
     {
     Parent_ID:Last(ParentList).ID
     }
     )
    )

     

    Regards,

    Mona

  • Verified answer
    james_hathaway Profile Picture
    427 on at

    Thanks Mona, I thought that was the case.

    Unfortunately, your solution isn't quite appropriate, as each item in the UpdateCollection will possibly need a different new Parent record - the Last(ParentList).ID variable would connect all child records to the same parent record...

     

    I believe I have to go to my other original idea and loop through the UpdateCollection several times; first to create the missing ParentList records, and then to Create the ChildList records, using a lookup to the updated ParentList to find the ParentID of the child record.

     

    Thanks anyway!

     

  • v-monli-msft Profile Picture
    Microsoft Employee on at

    Hi @james_hathaway ,

     

    Sorry that my suggestion didn't help and thanks for sharing your solution here.

     

    Regards,

    Mona

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 June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 320 Most Valuable Professional

#2
11manish Profile Picture

11manish 210

#3
Valantis Profile Picture

Valantis 167

Last 30 days Overall leaderboard