X++ Computed Columns and Virtual Fields in Data Entities
X++ Computed Columns and Virtual Fields in Data Entities
X++ Computed Columns and Virtual Fields in Data Entities
data entities
• 20/06/2017
• Tiempo de lectura: 6 minutos
•
o
This article provides information about computed and virtual fields, which are the two
types of unmapped fields that a data entity can have. The article includes information
about the properties of unmapped fields, and examples that show how to create, use,
and test them.
The sample code is targeted towards creating or modifying an entity that is a part of
solution that you own. Extending an existing entity requires slight modifications.
Overview
A data entity can have additional unmapped fields beyond those that are directly
mapped to fields of the data sources. There are mechanisms for generating values for
unmapped fields:
The two types of unmapped fields are computed and virtual. Unmapped fields always
support read actions, but the feature specification might not require any development
effort to support write actions.
Computed field
• Value is generated by an SQL view computed column.
• During read, data is computed by SQL and is fetched directly from the view.
• For writes, custom X++ code must parse the input value and then write the parsed values
to the regular fields of the data entity. The values are stored in the regular fields of the
data sources of the entity.
• Computed fields are used mostly for reads.
• If possible, it's a good idea to use computed columns instead of virtual fields, because
they are computed at the SQL Server level, whereas, virtual fields are computed row by
row in X++.
Virtual field
• Is a non-persisted field.
• Is controlled by custom X++ code.
• Read and write happens through custom X++ code.
• Virtual fields are typically used for intake values that are calculated by using X++ code and
can't be replaced by computed columns.
Properties of unmapped fields
PROPERTIES OF UNMAPPED FIELDS
Data IsComputedField NoYes Yes • Yes – The field is synchronized as a SQL view computed
column. Requires an X++ method to compute the SQL
definition string for the column. The virtual column definition is
static and is used when the entity is synchronized. After that,
the X++ method is not called at run time.
• No – The field is a true virtual field, where inbound and
outbound values are fully controlled through custom code.
PROPERTIES OF UNMAPPED FIELDS
Data ComputedFieldMethod String A static DataEntity method in X++ to build the SQL expression that
will generate the field definition. This property is disabled and
irrelevant if the property IsComputedField is set to No. The method
is required if the property IsComputedField is set to Yes.
In this example, you add a computed field to the FMCustomerEntity entity. For reads,
the field combines the name and address of the customer into a nice format. For writes,
your X++ code parses the combined value into its separate name and address values,
and then the code updates the regular name and address fields.
Nota
X++Copiar
In this example, you add a virtual field to the FMCustomerEntity entity. This field
displays the full name as a combination of the last name and first name. X++ code
generates the combined value.
1. In the designer for the FMCustomerEntity entity, right-click the Fields node, and
then click New > String Unmapped Field.
2. In the properties pane for the unmapped field, set the Name property
to FullName.
3. Set the Is Computed Field property to No. Notice that you leave
the DataEntityView Method empty.
Imagine that an external system sends the name of a person as a compound value that
combines the last and first names in one field that comes into our system. However, our
system stores the last and first names separately. For this scenario, you can use
the FullName virtual field that you created. In this example, the major addition is an
override of the mapEntityToDataSource method. When update is
called, mapEntityToDataSource methods are invoked for each data source.
1. In the designer for the FMCustomerEntity, right-click the Methods node, and
then click Override > mapEntityToDataSource.
2. Paste the following X++ code in for the mapEntityToDataSource method.
X++Copiar
The following main method tests your computed and virtual fields. Both fields are
tested in a read action, and the virtual field is tested in an update action.
1. For this example, ensure that you have the data set named Fleet Management
(migrated). The data set is available from the dashboard in the browser. Click the
menu icon in the upper-right corner, click the APP LINKS menu, and then scroll to
find the data set named Fleet Management (migrated).
2. Paste the following X++ code into the startup object of your project. Run your
project.
X++Copiar