Upgrade codeunits and the DataTransfer object
When a new version of your extension changes the data model, an upgrade codeunit moves the data. For large tables, the DataTransfer object does it far faster than a record loop.
The upgrade codeunit
codeunit 50110 "Issue Upgrade"
{
Subtype = Upgrade;
trigger OnUpgradePerCompany()
var
UpgradeTag: Codeunit "Upgrade Tag";
begin
if UpgradeTag.HasUpgradeTag(IssueTypeTag()) then
exit;
MoveIssueCategories();
UpgradeTag.SetUpgradeTag(IssueTypeTag());
end;
local procedure IssueTypeTag(): Code[250]
begin
exit('MYAPP-IssueType-20261017');
end;
}
OnUpgradePerCompanyruns once per company.OnUpgradePerDatabaseruns once in total.- There are also
OnCheckPrecondition...andOnValidateUpgrade...triggers. - Upgrade tags make sure the same step doesn’t run twice.
DataTransfer
local procedure MoveIssueCategories()
var
IssueCategory: Record "Issue Category";
IssueType: Record "Issue Type";
DT: DataTransfer;
begin
DT.SetTables(Database::"Issue Category", Database::"Issue Type");
DT.AddFieldValue(IssueCategory.FieldNo(Code), IssueType.FieldNo(Code));
DT.AddFieldValue(IssueCategory.FieldNo(Name), IssueType.FieldNo(Name));
DT.CopyRows();
end;
| Method | Purpose |
|---|---|
SetTables | Source and destination tables |
AddFieldValue | Map a source field to a destination field |
AddConstantValue | Write a fixed value into a destination field |
AddSourceFilter | Only copy matching rows |
AddJoin | Match rows between tables (for CopyFields) |
CopyRows | Insert new rows in the destination |
CopyFields | Update existing rows in the destination |
DataTransfer can only be used in upgrade and install code.
Learn more: Upgrading extensions and Transferring data using DataTransfer.
Practice questions
Pick an answer to check it. Questions are original and written for practice.
1. Which property makes a codeunit run during an extension upgrade?
Show answer
Answer: B.A codeunit with Subtype = Upgrade runs its upgrade triggers when a new version of the extension is installed over an old one.
2. You must copy a large number of rows from an obsolete table into a new table during upgrade. What is the fastest option?
Show answer
Answer: B.DataTransfer runs as one set-based SQL operation instead of row-by-row AL code. CopyRows inserts new rows in the destination table.
3. Which DataTransfer method updates fields in rows that already exist in the destination table?
Show answer
Answer: B.CopyFields updates existing destination rows, matched with AddJoin. CopyRows creates new rows.