cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
shalem120kjh
Frequent Visitor

The data source supplied to the function is invalid - runtime error with bulk patch

Hi,

First of all, it is important for me to say that I did search the forum and found some posts about this issue, but none of them helped me, so I'm forced to post my unique experience.

  • I'm caching a Dataverse table (Criteria) to a collection. 

 

 

 

ClearCollect(colCriteria,Criteria)​ ​

 

 

 

  • Presenting a filtered version of this table inside a gallery (criteria_Gallery). The "items" property is: 

 

 

 

Filter(colCriteria,Some_Filter)​​

 

 

 

  • Letting the user change some parameters of this Criteria with a control inside the gallery template (e.g. Applicable_toggle).
  • For a "Save" button, the "onSelect" property is bulk patching the updates back to the collection: 

 

 

 

Patch(colCriteria, 
ForAll(Criteria_Gallery.AllItems,
{Criteria: ThisRecord.Criteria, Applicable: Applicable_toggle.Value}))​​

 

 

 

  • This is where I get the runtime error "The data source supplied to the function is invalid".

Some important facts:

  • I'm using the lesser-used method of patching, with only 2 parameters: patch(source,rows) and not patch(source,baseRecord,recordChanges), as suggested here by @gabibalaban and also here.
  • If I patch directly to Dataverse table (replace colCriteria with Criteria), there are no errors and the data is saved in the table!
  • @RandyHayes - this is not just a runtime error in the designer, I also get it on the published app.
  • @StevenRichards - I have no idea how to implement your suggested resolution, because in my case there are no collections "in the middle".

Thanks for any help!

Gil.

6 REPLIES 6
StevenRichards
Helper II
Helper II

I can see an issue with how you're doing things for sure, but it's go nothing to do with my original issue. My issue is a known bug.

 

Just a suggestion:

You might want to consider patching the collection when toggled / untoggled for a given record rather than using a button at the end. When you're done doing all your toggling for the gallery, use the save button to patch all your records from the collection back to the datasource.

 

for the 'on check' property of Applicable_toggle you would use this: Patch(colCriteria,ThisItem,{Applicable: Applicable_toggle.Value}). For the 'on uncheck' property of Applicable_toggle do the same thing Patch(colCriteria,ThisItem,{Applicable: Applicable_toggle.Value}).

 

In the save button 'on select' property you could do a forall now that the collection has been updated from the patch commands you put in the toggle actions.

 

I don't want to push this on persay but you but you may want to consider forgoing the forall. It's slow.

 

If I were you I would use this instead for bulk patching: Patch(Criteria, ShowColumns(colCriteria,"Applicable"))

 

Bonus:

If you wanted to update more than just the Applicable field in your table from the gallery using additional controls, you would add the column to your save button patch command like this:

Patch(Criteria, ShowColumns(colCriteria,"Applicable","ApplesQty","AppleColor"))

 

Don't forget to patch during the change of a control as opposed to during the save button.

StevenRichards
Helper II
Helper II

My bad the save button code looks like this:

Patch(Criteria, ShowColumns(colCriteria,"ID","Applicable"))

 

shalem120kjh
Frequent Visitor

@StevenRichards , Thanks for the help and suggestions, and the bonus of course...

Your suggestion is actually how I first implemented my logic (I also follow @RezaDorrani ) , but since the Applicable_toggle also affects some other controls' visibility, there was a slight glitch/hiccup in the UI every time I toggled it, becuase of the constant patching to the collection.

 

As for the forall, in the way I implemented it, I don't see why it should be slow. As I said, when I update the datasource directly, it is working, and it is working fast. 

You wrote you see some issues with the way I do things, can you please elaborate what do you think is wrong, and why my way gives that error?

Thanks again

Gil.

StevenRichards
Helper II
Helper II

The obvious thing is that your patch statement needs to start with ForAll.

 

Check this out. It's exactly what you're trying to accomplish:

 

https://www.matthewdevaney.com/patch-multiple-records-in-power-apps-10x-faster/

 

It also explains how ForAll is slow.

 

You might be better off fixing your UI glitch in the long run.

 

Good Luck!

Also might need to use collect instead of patch if you insist on ForAll.

https://powerusers.microsoft.com/t5/Building-Power-Apps/Patch-all-items-in-Gallery/td-p/66288

 

 

WarrenBelz
Super User
Super User

Hi @shalem120kjh ,

Please note @StevenRichards 's post and use Collect if you are adding new records to a Collection, however the structure below (which you have) is far more efficient in collecting a Gallery Table in one command rather than looing through the Gallery one item at a time (so ForAll is inside the Collect). I also I tend to use an As statement for complete disambiguation.

Collect(
   colCriteria, 
   ForAll(
      Criteria_Gallery.AllItems As aCol,
      {
         Criteria: aCol.Criteria, 
         Applicable: aCol.Applicable_toggle.Value
      }
   )
)​​

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (1,573)