Data Entity Create Virtual Field – Microsoft Dynamics 365 for Finance and Operations

Scenario:
Requirement is to have a custom field showing Customer Number and Name.
Solution:
In that case instead of creating regular field on the Data entity and we will create a virtual field and fill the data at runtime when records are importing
Below are the steps needs to perform:
1. Create extension of the base entity (Example SalesOrderHeaderEntity) or your custom entity
2. Open the entity in designer and expand the fields
3. Right click and add new field -> StringUnmappedField
4. Set the Name property  of new field, in our case I put ‘CustomerNumberAndName’
5. Set the IsComputedField Property to No.
6. Leave the DataEntityViewMethod Empty
7.Use the virtual field to receive the information from outside(odata or excel) and parse the information
8. Use chain of command or pre-post event handler if its base entity or customise the method ‘mapEntityToDataSource’
‘example of mapEntityToDataSpurce’
9.
public void mapEntityToDataSource(DataEntityRuntimeContext entityCtx, DataEntityDataSourceRuntimeContext dataSourceCtx)
{
    next mapEntityToDataSource(entityCtx,dataSourceCtx);
    //Check if desired data source context is available
   switch (_dataSourceCtx.name())
{
case dataEntityDataSourceStr(SalesOrderHeaderV2Entity, SalesTable):
{
SalesTable salesTable = _dataSourceCtx.getBuffer();
this.CustomerNumberAndName = salesTable.InvoiceAccount + ‘ – ‘ salesTable.customerName();
}
break;
}
}
10. You can get the value from odata also to use it for different purpose as per request

The natural key for the table was not found. Microsoft Dynamics 365 for Finance and Operations – Microsoft Visual Studio

Scenario: Sometimes users facing the error ‘The natural key for the table was not found‘.

 

Screen Shot 2019-12-06 at 4.16.23 PM.png

 

Solution:

Open the table on which you are facing error. Then add the primary index like below

 

Screen Shot 2019-12-06 at 4.19.25 PM.png

 

After adding index synchronize the table and then try to create Data entity again.