Updating addresses in Dynamics 365 with an embedded Canvas App

Colin Maitland, 15 June 2021

In this article I will demonstrate how to update the addresses for a Contact in Dynamics 365 using a Canvas App that is embedded in a Model Driven App Contract.

This image shows the embedded Canvas App. The app is displayed on a tab named Addresses on a Model Driven App Contact Form. You learn how to embed a Canvas App into a Model Driven Form here.
In this example, the Address Form at the top of the screen displays the Primary Address for the current Contact and the Related Addresses Data Table at the bottom of the screen displays the related Addresses for the Contact. These addresses are sorted by the Address Number so that the Primary Address is always displayed at the top of the list. This ensures that the Primary Address is selected by default whenever the Data Table is refreshed because this is the address that is also displayed by the Address Form. In this example, the Address Form only ever displays the Primary Address.

clip_image002

This image shows the configuration of the DATA properties for the Address Form. [@ModelDrivenFormIntegration] is a reference to a component that is automatically added to a Canvas App when it is embedded into a Model Driven App form. [@ModelDrivenFormIntegration].Item provides read-only access to the current Contact displayed by the Model Driven Form.

clip_image004

The Default property of each of the Data Cards on the Address Form are associated with a corresponding variable. These variables are initialised whenever the user selects the tab on which the Canvas App is displayed. This is the code that initialises these variables.

clip_image006

This image shows the configuration of the ITEMS property of the Related Addresses Data Table. This configuration ensures that all the addresses related to the current Contact, excluding address number 2 and address number 3 if they are blank, are displayed. Notice that [@ModelDrivenFormIntegartion].Item.Contact is used to filter the addresses by their ‘Parent’ Contact. Address number 2 and address number 3 correspond to the second and third sets of address fields for Contacts in Dynamics 365. These are excluded if they are blank.

clip_image008

If the user edits the Address displayed at the top of the form, Save and Cancel buttons are displayed to allow the user to save or cancel their changes. Optionally, a hidden timer can also be used to automatically save any changes after a predetermined period.

clip_image010

The Save button invokes the following action:

clip_image012

After the form has been submitted, the OnSuccess and OnFailure events on the form then cause a success or failure notification to be displayed to the user.

The Cancel button invokes the following action:

clip_image014

If the user selects an Address, other than the Primary Address, from the list of Related Address then an Update button is displayed to allow the user to update the Primary Address from the selected Related Address.

clip_image016

This is an example of the formula used to update the Primary Address from the selected Related Address. The variables being updated by this formula are associated with the Default property of each of the Data Cards on the Address Form. Whenever these variables are updated, the corresponding controls on the Address Form are also updated because their Default property references the Default property of their parent Data Card; i.e. Parent.Default.

clip_image018

In addition, depending on the business requirements, the current Primary Address if not blank, could be swapped with the selected Related Address as part of the update process. This would ensure that the Primary Address to be replaced is not lost when it is updated from the selected Related Address. This image shows the addresses after they have been swapped. The first address in this list is the updated Primary Address and the last address is the previously selected Related Address that has now been replaced from the previous Primary Address.

clip_image020

Here is an example of a Patch function that updates the selected Related Address from the Primary Address. Notice that because this is an update of an existing address, the ‘customeraddressid’ is specified using the ‘customeraddressid’ of the selected Related Address. The other address columns are updated from the Primary Address of the current Contact accessed using [@ModelDrivenFormIntegration].Item.

clip_image022

Here is an example of a Patch function that would create a new address in Related Addresses from the Primary Address. Notice that because the ‘Parent’ field is a polymorphic lookup field, the Contact to which the new address is to be associated needs to be retrieved from the [@Contacts] data source.

clip_image024

In conclusion, this article provides insight into the configuration of the data sources and formulas used to update addresses for a Contact using a Canvas App that is embedded in a Model Driven App form in Dynamics 365.