Ever encountered problems with loading data in your Canvas app OnStart property when embedding it in a Model-Driven-App? While it usually works great, I found one situation where you have to put in some additional steps to ensure everything is loaded correctly. Time for a Quick Tip πŸ’‘

❓ Using ModelDrivenFormIntegration.Item as filter

In my app I want to embed a Canvas app on the main form of my entity Budget. Each Budget has n Budget Entries which are linked using a one-to-many relationship. The Canvas is supposed to display the Budget Entries in a more graphical and editable way. For that I need to do some modifications to them which I why I loaded them in a collection. My first try was using the “OnStart” property of the Canvas app:

OnStart: ClearCollect(collEntries, [@ModelDrivenFormIntegration].Item.'Budget Entries')

This works great in the editor and when you trigger the “OnStart” everything shows up as you would expect. The collection collEntries is used as item source for the shown gallery and shows up as soon as you run “Run OnStart”.

But when we embed this Canvas app on the Budget form the result is disappointing, the gallery remains empty πŸ™„

After a bit of try and error I discovered the cause for this. In the OnStart of your app the ModelDrivenFormIntegration object is not yet filled. This means for embedded apps you can not use this trigger for data loading where you want to filter based on record the app is embedded on.

βœ… Alternative initial loading method

There are different ways on how to solve this, in the past I used a timer control to trigger the initial load a bit after the start, but this always felt “hacky”. Recently I discovered a more simple and straight forward way πŸŽ‰

For this we are going to utilize one of my favorite controls, the Toggle control. The toggle control offers us a Default Value and a trigger OnChange when its value changed. The idea behind using this for initial data loading is the following: We will use a property of the ModelDrivenFormIntegration.Item as default value and do the data loading in the OnChange trigger.

Default: IsBlank([@ModelDrivenFormIntegration].Item.Budget)
OnChange: ClearCollect(collEntries, [@ModelDrivenFormIntegration].Item.'Budget Entries')

You can hide this toggle, it’s only job is to trigger when the ModelDrivenFormIntegration.Item is available. Why does it work? πŸ€” When the app is initially loaded the ModelDrivenFormIntegration.Item is blank and because of that our Default value is true, because the Id of the record is indeed blank. As soon as the ModelDrivenFormIntegration.Item becomes available, the Default value changes to false which triggers the OnChange. And with that our data is loaded even when the app is embedded πŸ‘