Update Model-driven Record from Embedded Canvas App

April 16, 2020

 

 

I recently blogged about embedding a canvas app into a Model-driven app form and the potential that it unlocks. You can create really complex functionality, easily with canvas apps but you need to be able to write the data back to your Model-driven form to really unlock the power and potential. Let's go through how to update a Model-driven record from inside the canvas app.

 

You can follow my guide here<link to article> to embed a canvas app into a Model-driven app form. You can update the form in a variety of ways but one of the simplest is to use a button. In your canvas app, click on insert and then button.

 

 

 

Once the button is on the canvas you can make any of the changes you want to it, change the text, the colour, the fade etc etc. The important part is OnSelect action of the button, in this we are going to add our formula.

 

 

 

The OnSelect action will run when the button is pressed/selected so this is how we will trigger an update to the Model-driven form. Next we need to write a formula to update the record and we will be using a Patch() statement. Patch allows you to update or create records, you just pass in the parameters. The syntax for a Patch statement is:

 

Patch(<Source table>, <Record>, <Fields to update>...)

 

The ellipses above represents that you can have any number of fields to update.  The Source is the table which we want to update in the CDS database. The record is the Record itself you want to update (not a GUID, that’s important). The fields to update are the fields you want to write data to in CDS.

 

Breaking down this more granularly for CDS, the source table needs to be the plural name for the entity and not the schema name. For example use ‘Quotes’, ‘Quote Products’, ‘Accounts’, ‘Contacts’ to update those records.

 

Record is not the GUID of the record you want to update, but the record itself. This is a little unusual as most people with any sort of integration would be more used to using GUIDs to update records and this caught me out a little as well. If you are using a Canvas app embedded into a Model-driven app, then you have the ModelDrivenFormIntergration that you can use for this. To get the record, you can use ModelDrivenFormIntegration.Item and that is the record you are currently in.

 

The fields to update are the field labels on the Model-driven form, again, not the schema names, but the labels. The values to write to these fields are separated by colons : and if there is more than one field, it should be in braces, then each line is separated via a comma. 

 

Example of patch statement is below and updates quote products with fields on a canvas app.

 

Patch('Quote Products', ModelDrivenFormIntegration.Item,

   {

   'Additional Exchanged Cost' :Value(LabConvertedCost.Text),

   'Exchange Rate Today' :Value(LabExchangeRate.Text),

   'Translated Text':TxtTransLatedText.Text,

   'Exchange Rate Calculated': Today(),

   'Currency Code': DrpCurrencySel.SelectedText.Value

   }

)

 

The other important thing to note is that you need to ensure the types of fields you are mapping, match the fields in your Model-driven app. For instance, if you want to update a date field, you need to use date field data, and not a string.

 

 

 

You can also save the Model-driven app form at the same time, simply add a semicolon at the end and use the ModelDrivenFormIntegration.SaveForm() function and it will save the record with all these new values.

 

Patch('Quote Products', ModelDrivenFormIntegration.Item,

   {

   'Additional Exchanged Cost' :Value(LabConvertedCost.Text),

   'Exchange Rate Today' :Value(LabExchangeRate.Text),

   'Translated Text':TxtTransLatedText.Text,

   'Exchange Rate Calculated': Today(),

   'Currency Code': DrpCurrencySel.SelectedText.Value

   }

);

ModelDrivenFormIntegration.SaveForm()

 

There you have it, an easy way to pass data from a canvas app back to a Model-driven app and save it.

 

Ciao for now!

 

Please reload

Recent Posts

Please reload

Archive

Please reload

Tags

D365 Blog by Matt. ©2018