How to extend a data entity to add a custom virtual field in Dynamics 365 Finance & Operations (D365FO)

Posted by Sam on Tuesday, June 14, 2022

Showing how to extend or customize a data entity to add a custom virtual field in Dynamics 365 Finance & Operations (D365FO)

Introduction

It is a common request to customize a standard data entity. In this post, we will explore how you can do that in D365FO.

In this particular example, I’m adding a custom virtual field which is an unmapped field.

  • Mapped fields are mapped directly to fields of a data source.
  • Unmapped fields include 2 types:
    • Computed fields are generated by SQL
    • Virtual fields are generated by custom X++ code

Reference: recommend to read the official document of Computed columns and virtual fields in data entities which explains in more details about the differences between two types of unmapped fields.

Note: The prerequisite is that you’ve already known how to create a model, a Dynamics 365 project in Visual Studio to follow along the below steps.

How to Add a Custom Virtual Field

  1. In Visual Studio, open your project under your own custom model. Then in AOT window, find the standard data entity that you want to extend so as to create extension of it.

    Open the extended data entity to add a new unmapped field

    Add a new unmapped field in the extended data entity

  2. Checking out the properties of the newly added virtual field and ensuring that the Is Computed Field is No. Setting Is Computed Field property to No of the new custom virtual field

  3. One important step is to remember extending the staging table to add a new field as well. Otherwise, even though you might build your project successfully, the field will not show up and work correctly. Extending the corresponding staging table to add the new field

    Extending the corresponding staging table to add the new field

  4. A reminder is a virtual field is generated by custom X++ code. And the logic of populating values for the custom virtual field is controlled by a method called postLoad(). Because it’s not possible to override the entity code directly, we will need to create an extension class like below. Creating an extension class of the data entity to adding logic of postLoad()

  5. Now we can build and synchronize to database our custom project.

    Note: that you might get some references errors (depending on your current custom model’s references) -> Just update your custom model with references to those models containing the missing/not found elements. Build and sync your custom project)

  6. Check out the result

    • Via Data Management workspace in the client: remember to refresh data entity list then check out the entity.
    • Via Odata, I can quickly check out the data in browser by opening the url http://[yourEnvironmentURI]/data/CustomerGroups Seeing the newly added virtual field with expected result via Odata)