Dynamics 365 Business Central 2023 wave 1 (BC22) is generally available last week. More details: Dynamics 365 Business Central 2023 release wave 1 (BC22)
I will continue to test and share some new features that I hope will be helpful.
Using a new language construct, developers can write faster install code when they want to move a field (with data) from one table to another, or when they want to copy all data from one table to another.
When refactoring an app, developers often need to move a field from one table to another, or to copy all data from one table to another. With this feature, a developer can copy the data from the old field to the new field, or copy the data from the old table to the new, in a set-based way without having to write a loop.
With DataTransfer, install code runs significantly faster.https://learn.microsoft.com/en-us/dynamics365/release-plan/2023wave1/smb/dynamics365-business-central/developers-write-faster-install-code
This is an improvement to the following feature in 2022 release wave 2 (BC21).
More details: Developers can write faster upgrade code (New DataTransfer data type)
In 2022 release wave 2 (BC21), the DataTransfer data type can only be used in upgrade code. With this wave, we can also use DataTransfer data type in Install Codeunits (Subtype = Install).
Let’s briefly review what is DataTransfer Data Type.
DataTransfer is an AL data type that supports the bulk transferring of data between SQL based tables. Instead of operating on a row-by-row model, like the record API does, DataTransfer produces SQL code that operates on sets. This behavior improves the performance when moving data during upgrade and install.
DataTransfer Data Type: A structure building bulk transfer of data between tables.
The following methods are available on instances of the DataTransfer data type.
|AddConstantValue(Any, Integer)||Specifies the given value is to be set in the given field in the destination table.|
|AddFieldValue(Integer, Integer)||Specifies a source and destination field, where the values from the source field are to be copied to the destination field. The data types of the fields must match, except CODE to TEXT which is allowed.|
|AddJoin(Integer, Integer)||Adds a field pair to be used to create a join condition which determines which rows to transfer, optional for same table transfers.|
|AddSourceFilter(Integer, Text [, Any,…])||Adds a filter for the source table for the data transfer.|
|CopyFields()||Copies the fields specified in AddFields with filters from AddSourceFilter, and the join conditions from AddJoins in one bulk operation in SQL.|
|CopyRows()||Copies the rows from the source table to the destination table with the fields selected with AddFields and the filters applied with AddSourceFilter, in one bulk operation in SQL.|
|SetTables(Integer, Integer)||Sets the source and destination tables for the data transfer.|
|UpdateAuditFields([Boolean])||Sets if audit fields should be updated. If the value is set to false, the audit fields are not updated when calling the CopyFields method. Default value is true.|
The DataTransfer object can be used for essentially two operations:
- Copy data from one or more fields in a table to fields another table. A typical scenario is when you’ve made a field obsolete.
codeunit 104021 “Upgrade Item Cross Reference”: Database::”Purchase Line”
- Copy data from entire rows in a table to rows in another table. A typical scenario is when you’ve made a table obsolete.
codeunit 104000 “Upgrade – BaseApp”: Database::”Dimension”and Database::”Dimension Value”
PS: The DataTransfer object can’t be used on the following tables:
- Non-SQL tables
- System tables
- Virtual tables
- Audited tables as the destination
- Obsoleted tables as the destination
More details in MS Learn (Docs): Transferring Data Between Tables using DataTransfer
Next, let’s look at a simple example.
I need to pass the value of Name field on Customer to Name field in my new table when the extension is installed.
Another example, I need to pass the value of the Name field in the Customer table to my newly created ZYName field in the same table.
Very simple, give it a try!!!😁
PS: In the previous versions, if you try to use the DataTransfer data type other than in Upgrade Codeunit, it will not generate a compile error, but it will throw a runtime error.
In BC22, this error message is also updated.
DataTransfer is only usable during upgrade and installation code.
Note: Usage and behavior
Hope this will help.
Thanks for reading.