Infamous "The natural key for some table was not found" solved once and for all

Data entities in Dynamics 365 for Finance and Operations (D365FO) are awesome, as long as you can use the wizard to create them. Quite often you will find that some tables throw the infamous "The natural key was not found". This is because many tables actually don't have unique indexes - for many reason - but here I'll present a general solution to create any entity - read-only entities - in very simple steps. 

Capture when trying to create an entity for InventTrans table


In this example I'll create an entity for the InventTrans table as this table doesn't have an unique index. 

Step 1:

Use the wizard to create an entity but select any know table that happens to have unique indexes. In this case I'll select InventTable. 


In the next screen Deselect all the fields and click Finish.



You will end up with an entity with a single datasource



Step 2: 

In this step we need to create a View that is based on the table that we want to expose as an entity - in this case InventTrans. The view will allow us to create an unique index. 

Add InventTrans table as datasource and select the fields for the view to return. 



Step 3:

Add an unique index to the view. This is the step that will make the trick. 

Depending on the entity you can choose or create the index based on a combination of fields or just use RecId as unique index. As this example if only for read-only purpose entities I will just use RecId. 


Save the view. 

Step 4:

Go back to the entity and remove the references to InventTable - or whatever table you choose - and drag and drop the View as DataSource


Move the fields from the DataSource to the EntityFields


Last but not least,

Regenerate the stagging

Step 5: 

Build, DB Sync and Test


 









Comments

Popular posts from this blog

Telemetry and Monitory with Applications Insights and Dynamics 365 F&O

Creating a DMF data package from files stored in an Azure blob storage