How to clear Lookup fields of CDS from Canvas app.

This one is pretty interesting and also the one which took me by surprise. The requirement was very simple – I have a canvas app screen from where I needed to update back CDS account record.

Apart from few other fields, I also needed to clear the Primary contact field on Account which is basically a lookup field . Since I was not using the form control, I has to use the Patch function to update the record.

While reasonably simple, there is a trick to it and in this blog I am going to discuss the same. The example here will cover these following scenarios.

  • You want to clear out the Lookup field values in CDS from Canvas app using Patch function.
  • You want to clear the polymorphic fields of CDS like Customer/ Owner.
  • You need to clear the person field of SharePoint from Canvas app.

You might have noticed that I have mentioned to clear out the Person field type of SharePoint. While my example will focus on CDS lookup field only, the same technique can be used for SharePoint person field.

I thought it would be walk in the park. And below is the code I started with.

Patch(Accounts, {'Account Name': TextName.Text, 'Account Number': TextNumber.Text, 'Primary Contact':Blank(), Account: Gallery1.Selected.Account})

Not much to understand here. Updating a few values for Account fields including the Primary contact which I am trying to set to null. When I run this code, it works fine without errors and other fields with values are getting updated. However unfortunately the Primary contact field didn’t get updated.

Quite surprised actually on why that happened. Searched the heck out of Google and stumbled upon a Microsoft post which suggested to enable a setting for Canvas app to make this work.

Go to Settings -> Advanced Settings and check for the setting – Formula-level error management under Experimental features. It’s set to off by default. Enable it and then save the settings.

Now when I run the below code, it works just as expected.

Patch(Accounts, {'Account Name': TextName.Text, 'Account Number': TextNumber.Text, 'Primary Contact':Blank(), Account: Gallery1.Selected.Account})

Using the same procedure you and update the Sharepoint Person field or any other Polymorphic field like Customer, Owner of CDS to clear out its value.

You may also like the below posts

Hope this helped!

Debajit Dutta
Business Solutions MVP