Upgrade codeunits and the DataTransfer object

MB-820 · Install, develop and deploy extensions · Updated

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;
}

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;
MethodPurpose
SetTablesSource and destination tables
AddFieldValueMap a source field to a destination field
AddConstantValueWrite a fixed value into a destination field
AddSourceFilterOnly copy matching rows
AddJoinMatch rows between tables (for CopyFields)
CopyRowsInsert new rows in the destination
CopyFieldsUpdate 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.

Keep going