Options

How to use upgrade codeunits?

emerikspemeriksp Member Posts: 10
edited 2018-11-21 in NAV Three Tier
Hi,

Upgrade codeunits are a relatively new concept for me, and I am in need of some help :-)

Let's say:
I have a table with the following structure:

1 Primary Key Code(10)
2 DescriptionText30 Text(30)
3 DescriptionText50 Text(50)

Now I want to change field 3 from Text(50) to Code(50). This would be a destructive change in NAV 2017 CU1 and forcing that change would result in the loss of the data contained in that field.

Can I solve this problem with an upgrade codeunit? How do I do that, exactly?

Thanks for your help.

Answers

  • Options
    AKAK Member Posts: 226
    edited 2018-11-22
    A codeunit can't change the type of a field, that always has to be handled by importing a new object or changing it manually. The process typically works like this:

    1. Create a copy of your table
    2. Copy all records to it
    3. Delete either all records from the original table or just the contents of field 3
    4. Change type of field 3 in original table
    5. Copy data back from the intermediate table according to the new field type

    Steps 2,3 and 5 could be done by codeunits.
  • Options
    joerg_renzjoerg_renz Member Posts: 29
    Hi,

    1. Create a copy of your table with the primary key field(s) and fields, that will be changed (e.g. from Text to Code). You simply can create an exact copy of your (source) table. This copy will be used to hold the existing records from your source table.
    2. Create a new codeunit. The Subtype of this new codeunit must be "Upgrade".
    3. Create a new function in this upgrade-codeunit. The "FunctionType" of this new function must be "TableSyncSetup".
    4. In the new codeunit, create a new global var for codeunit 9900 "Data Upgrade Mgt."
    5. In the new function from step 3, copy your existing source records to your table-copy with function "SetTableSyncSetup" from codeunit 9900
    6. e.g. DataUpgradeMgt.SetTableSyncSetup(DATABASE::"My source table",DATABASE::"My copy table",
    TableSynchSetup.Mode::Copy);
    7. Create a new function in your upgrade-codeunit. The "FunctionType" of this new function must be set to "UpgradePerCompany" or "UpgradePerDatabase" - depending of your source table is "DataPerCompany=yes" oder "DataPerCompany=No"
    8. In this new function, you can transfer the records from your table-copy to your source-table using the normal C/Side functions (repeat until, get, modify)
    9. modify field 3 in your source table from type "Text" to "Code"
    10. Save your modified table using "Synchronize Schema" with option "now - with validation"
    11. Now you can execute your upgrade-codeunit using "Tools" - "Data Upgrade" - "Start..." in DevClient
    12. Check your data in your source table
    13. delete your table-copy using "Synchronize Schema" with option "force"
Sign In or Register to comment.