How to update a record in CDS/ Dynamics 365 from canvas app without using primary key

Follow Debajit’s Power Apps & Dynamics 365 Blog on WordPress.com

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.

image

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.

image

Formula:

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 www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading