Development Tips

Data Upgrade – in Navision 2015

Microsoft Dynamics NAV 2015 introduces a new way of running the data upgrade logic that migrates data from the old table structure of an earlier Microsoft Dynamics NAV version to the new table structure of the current version.

The data upgrade process is now prepared and run with the help of new type of codeunit called the upgrade codeunit. The data upgrade process has been enhanced significantly to optimize the performance and provide developers with a superior tool for testing, automating, tracking progress and troubleshooting the data upgrade code. The new data upgrade approach has the following benefits and features:

  • Simplifies the upgrade environment because all steps are performed by the latest version of the Microsoft Dynamics NAV.
  • Increases the data upgrade performance because it is possible to run upgrade functions of upgrade codeunits in parallel (at the same time) and across companies.
  • Reduces the amount of code that is required to handle the data upgrade because several operations are now executed automatically by Microsoft Dynamics NAV Server.
  • Minimizes the number of manual actions that must be performed during the data upgrade, which makes the data upgrade process less error-prone.
  • Uses familiar upgrade toolkit design concepts, such as upgrade tables and upgrade functions.
  • Includes end-to-end sample Windows PowerShell scripts that perform a complete data upgrade.

Please check my earlier post on this topic for more detailed information.

Schema Synchronization in Microsoft Dynamics Navision 2015

Data Upgrade Codeunit in Navision 2015 Part-1

Data Upgrade Codeunit in Navision 2015 Part-2

Development Tips

Data Upgrade Codeunit in Navision 2015 – Part -2

We have seen how to create Upgrade code unit in our previous post.

Please go through it, if not seen earlier the Data Upgrade Codeunit in Navision 2015 – Part -1 here 

Let us continue from where we left our previous post.

We have created the Upgrade code unit to copy the data to upgrade table while performing destructive changes to My Sales Customer table.

We removed certain fields from the My Sales Customer table and the related information was moved to UPG My Sales Customer table.

Now it’s time to move these information to actual table we create for this purpose My Detailed Customer Entry table.

Although in this example this table will be exact copy of UPG My Sales Customer table, so simply we need to copy records to My Detailed Customer Entry table.

But some time we may require to add certain more field’s value while moving the data Like Entry No, Posting date to maintain the data integrity. Like think of some Service type of information you are maintaining then the same set of record may repeat number of times on different dates, then in this case we will require to add certain fields like Entry No. and Posting Dates.

Let us assume we will be maintain data of My Detailed Customer Entry in incremental way as of situation on every transaction. Balances will be maintained as of when transaction was performed.

Although this example is not feasible or practice but still we are continuing to just see how to write Upgrade Function for such purpose.

Our today’s task is to design and run data upgrade function to move data from the upgrade table to its new location.

Now the fields are deleted from the My Sales Customer table and the data is saved in the UPG My Sales table. The next step is to create the new table where the data will reside (My Detailed Customer Entry) and move the data into that table.

Let’s create the table to be used for recording of all future Customer Balances and to accommodate the data we already had in the My Sales Customer table and which we moved into the UPG My Customer table.

  • In the Object Designer select the Tables button and then select New button to create a new table
  • Define the table layout based on the layout shown in the Screenshot.

Upgrade Codeunit 2 - 1

  • In the Dev. Environment menu, select File-Save action to save the table.
  • On the Save dialogue, define ID, Name = My Detailed Customer Entry, and select OK to save the table.
  • Close the table editor.

We will now create a new function which will contain the code to migrate the data from the upgrade table to the new table.

Note that the FunctionType property of the newly created function is set to “Upgrade”. This is how the NAV server can later figure out which function from the upgrade codeunit it must execute.

The function marked as “Upgrade” function can call functions marked as “Normal” if necessary.

  • In the Object Designer select the Codeunits button, select codeunit 50000 UPGTK Codeunit and then select the Design button.
  • In the Dev. Environment menu, select View-C/AL Globals action and in the form which is opened select the Functions tab.
  • Enter the name of the function, e.g. UpgradeCustomerDetails

Upgrade Codeunit 2 - 2

  • Select the UpgradeCustomerDetails function on the Functions tab and then select the Locals button to define function parameters.
  • On the C/AL Locals form, select Variables tab and add 3 local variables as shown in the Screenshot section.
  • Close the C/AL Locals and C/AL Globals forms to return to the C/AL Editor.

Upgrade Codeunit 2 - 3

The code we add to the UpgradeCustomerDetails function will copy the data from the UPG My Sales Customer table to the My Detailed Customer Entry table and then clean up the UPG Vehicle table.

  • Code like the Screenshot into the UpgradeCustomerDetails function.

//>> Upgrade Code

EntryNo := 1;

WITH UpgCustomer DO BEGIN

IF FINDSET THEN

REPEAT

DetailedCustomerEntry.INIT;

DetailedCustomerEntry.”Entry No.” := EntryNo;

DetailedCustomerEntry.”Customer No.” := “Customer No.”;

DetailedCustomerEntry.”As of Date” := today;

DetailedCustomerEntry.”Last SO Value” := “Last SO Value”;

DetailedCustomerEntry.”Last Invoiced Value” := “Last Invoiced Value”;

DetailedCustomerEntry.”Total Outstanding Value” := “Total Outstanding Value”;

DetailedCustomerEntry.INSERT;

EntryNo := EntryNo + 1;

UNTIL NEXT = 0;

DELETEALL;

END;

//<<Upgrade Code

  • In the Dev. Environment menu, select File-Save action to save the codeunit.
  • On the Save dialogue select OK button.
  • Close the codeunit editor.

Upgrade Codeunit 2 - 4

  • In the Dev. Environment menu select Tools – Data Upgrade – Start… action.
  • Agree to the confirmation dialogue.
  • In the Start Data Upgrade window select the OK button.

You can start the data upgrade process from the Dev. Environment, or from Microsoft Dynamics NAV 2015 Administration Shell, using Start-NAVDataUpgrade cmdlet.

The NAV server will locate all upgrade codeunits and will invoke all upgrade functions within these codeunits for all companies in the database.

If you have multiple upgrade functions in your upgrade codeunit, you can choose to execute them in parallel or in sequence. You can also choose to stop at first error, or continue execution of other functions, see the list of all failed ones, fix them and start them again using the Data Upgrade – Resume… action.

  • In the Dev. Environment menu select Tools – Data Upgrade – Start… action.
  • Agree to the confirmation dialogue.
  • In the Start Data Upgrade window select the OK button.

Upgrade Codeunit 2 - 5

Upgrade Codeunit 2 - 6

  • In the Object Designer select the Tables button, then select table My Detailed Customer Entry and select Run button

Upgrade Codeunit 2 - 7

You can see that the data from the upgrade table was successfully transferred into the new table.

Now since our data is sitting in its right place, now we can remove objects we are using for Upgrade purpose.

Now the data upgrade is completed and we need to remove the objects used within that process.

Force-delete the upgrade table, since the data was already transferred into the new table.

Delete the upgrade codeunit to make sure the old synchronization instructions defined in it are not used by the NAV server when you decide to change the My Sales Customer table again. Alternatively (if you want to reuse the upgrade codeunit later) you can choose to change the type of the GetTableSynchSetup function to “Normal”.

Upgrade Codeunit 2 - 8

Now we are done with our Upgrade project.

Conclusion

This set of demonstrations have provide you the insight, new way of running the data upgrade in Microsoft Dynamics NAV 2015 by involving the schema synchronization and upgrade codeunits.

The new approach:

  • Simplifies the upgrade environment, since all steps are performed by the latest version of the product.
  • Increases the data upgrade performance, since it is possible to parallelize execution of the data upgrade functions within the upgrade codeunit and across companies.
  • Reduces the amount of code to be written to handle the data upgrade, since some actions are now executed automatically by the NAV Server.
  • Allows data upgrade to be invoked by the NAV server, minimizing the number of manual actions which should be performed during the data upgrade.
  • Uses familiar upgrade toolkit design concepts (e.g. upgrade tables, upgrade functions).
Development Tips

Data Upgrade Codeunit in Navision 2015 – Part -1

Let us start with background preparation.

I have prepared a table with below fields and filled with some sample data.

DataUpgradeCU-1

Fill in the My Sales Customer table with demo data.

Make sure you have data in all columns, preferably create several records in this table.

Now when the fields are deleted from the My Sales Customer Table and the data is saved in the UPG My Sales Customer table, we will create the new table where the data will reside in the new version (My Detailed Customer Entry) and move that data into that table using Data Upgrade action in the Dev. Environment.

Since now my project requirement is to refactor the My Sales Customer table and delete the fields which store the information about the Sales Information (“Value” fields), we need to create an intermediate upgrade table where the data from those fields will be stored until you move it into the new My Detailed Customer Entry table.

We will perform below action:

  • Open the Object Designer and select table My Sales Customer
  • Select the Design button to open the table in the designer.
  • In the Dev. Environment menu, select File-Save As… action.
  • Change the ID value to New ID, change the Name to UPG My Sales Customer and select OK button to close the dialogue and create a copy of the table.

DataUpgradeCU-1

  • In table UPG My Sales Customer delete fields Customer Name, Customer Type and Credit Limit.
  • In the Dev. Environment menu, select File-Save action.
  • On the Save dialogue, set the Synchronize Schema option to “Force” and  select OK button to close the dialogue.

DataUpgradeCU-1

  • In the Object Designer select the Codeunits button and then click New to start creating a new upgrade codeunit.
  • In the Dev. Environment menu, select View-Properties action to start editing the properties of the codeunit.
  • Change the Subtype property to Upgrade and close the Properties form.

DataUpgradeCU-1

  • In the Dev. Environment menu, select View-C/AL Globals action and in the form which is opened select the Functions tab.
  • Enter the name of the function, e.g. GetTableSyncSetup
  • In the Dev. Environment menu, select View-Properties action to start editing the properties of the function.
  • Change the FunctionType property to TableSyncSetup and close the Properties form.

DataUpgradeCU-1

  • Select the GetTableSyncSetup function on the Functions tab and then select the Locals button to define function parameters.
  • Add a parameter of DataType = Record, Subtype = Table Synch. Setup, Name = TableSynchSetup, Var = Yes.

DataUpgradeCU-1

  • On the C/AL Locals form, select Variables tab and add a local variable of DataType = Codeunit, Subtype = Data Upgrade Mgt.
  • Close the C/AL Locals and C/AL Globals forms to return to the C/AL Editor.

DataUpgradeCU-1

  • In the GetTableSyncSetup function, add the C/AL statement as presented on the screenshot.
  • In the Dev. Environment menu, select File-Save action to save the codeunit.
  • On the Save dialogue, define ID , Name = UPGTK Codeunit, and select OK to save the codeunit.
  • Close the codeunit editor.

DataUpgradeCU-1

[

//>>Data Upgrade Code

// To copy data from table My Sales Customer to UPG My Sales Customer table

DataUpgradeMgt.SetTableSyncSetup

(

DATABASE::”My Sales Customer”,

50002,

TableSynchSetup.Mode::Copy

);

//<<Data Upgrade Code

]

  • In the Object Designer, select the Tables button and then select the My Sales Customer table in the list of tables.
  • Click Design to open the table in the table designer.
  • Delete the following fields:
  • Last SO Value
  • Last Invoiced Value
  • Total Outstanding Value
  • In the Dev. Environment menu, select File-Save action.
  • On the Save dialogue, notice that the Synchronize Schema option is set to “Now – with validation” and select OK button to close the dialogue.

DataUpgradeCU-1

  • In the Object designer, find table UPG My Sales Customer and click Run to check if the data was copied there by the schema synchronization.

DataUpgradeCU-1

Here we are now done with the Part -1 of our exercise. Next step is to move this temp data in respective table as part of our Upgrade. We will see upgrade part in our next post.