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

Patch function within a Patch function

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Helper I
Helper I

Re: Patch function within a Patch function

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!

 

View solution in original post

3 REPLIES 3
Highlighted
Community Support
Community Support

Re: Patch function within a Patch function

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

Community Support Team _ Mona Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Helper I
Helper I

Re: Patch function within a Patch function

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!

 

View solution in original post

Highlighted
Community Support
Community Support

Re: Patch function within a Patch function

Hi @james_hathaway ,

 

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

 

Regards,

Mona

Community Support Team _ Mona Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
secondImage

Demo-Extravaganza 2020

Check out these cool Power Apps & vote on your favorite!

secondImage

Robotic Process Automation

Let's talk about the solution provided by Microsoft for Robotic Process Automation (RPA)

secondImage

Community Highlights

Check out whats happening in Power Apps

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

Top Solution Authors
Top Kudoed Authors
Users online (9,620)