cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
james_hathaway
Level: Powered On

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
james_hathaway
Level: Powered On

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
Community Support Team
Community Support Team

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.
james_hathaway
Level: Powered On

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

Community Support Team
Community Support Team

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
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (4,788)