Using Patch function in canvas app to update a CDS record based on value of non-primary key field.

I have recently been conducting some trainings on canvas apps and one of the questions that came up during the session was regarding the Patch function in canvas app.

As the name suggests, you can use the Patch function to modify or create a record in datasource. If you are unaware of the Patch function in canvas app, I suggest to go through the Microsoft Docs link.

However the situation I am going to explain here is bit different. The situation was more of like this.

“There is a contact form spanning multiple screens. The user would enter all the details of the contact including the email address. Depending on the value of email address, an insert or an updated would be performed using the patch operation”.

If I analyze the requirement, basically I would require check if there is any existing contact with the same email address. If exists then I should update, otherwise I should create. Pretty simple huh? Yes it is simple. But specific example is missing in docs. Hence penning this blog in case it is helpful for someone.

For this example:

Datasource – Contacts table of CDS. However this logic can be applied to any data source.

I have kept a really simple single screen here. Mind you I haven’t used a Edit form or a create form. The real requirement was in-fact spanning multiple screens for collecting contact info but for this example I have kept it simple.

Below is the screenshot of the same. A very simple one indeed.


Once the button “Update to CRM” is clicked, the magic should happen. To meet the requirement, I use the below formula in the “On Select” property of the button.



Patch(Contacts, {contactid : LookUp(Contacts, emailaddress1 = EmailText.Text).Contact}, {firstname: FirstName.Text, lastname: LastName.Text, address1_city: City.Text, emailaddress1: EmailText.Text, address1_stateorprovince: ContactState.Text});

If you observe the above formula carefully, I am using the LookUp function to search for a contact with a emailaddress and then getting the contact id. If there is no matching email, contactid would be blank and hence the record is created.

For matching contact it is updated.

Now the question arises – What if there are multiple records which match the criteria? In that case the LookUp function would return a reference to just one of them and the returned record would be updated.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit or reach out to us at

Our product offerings:

Role based views for Dynamics 365 (

CRM-Sharepoint Attachment uploader and metadata manager (

Record Cloner for Dynamics 365 (

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s