In this video I show an example of how to reuse old NAV code in Microsoft Dynamics 365 Business Central.
In this video, Erik demonstrates how to take legacy NAV code — specifically an 11-year-old barcode generation codeunit — and convert it to work in modern Business Central using AL. The process involves using Microsoft’s txt2al tool, cleaning up object references, and handling the character encoding differences between the old 8-bit NAV environment and Business Central’s Unicode runtime.
The Starting Point: Old NAV Barcode Code
The story begins with a practical need: barcode printing in Business Central. Erik and a colleague remembered that there used to be a way to generate barcodes directly in C/AL code — no external libraries or fonts required. They tracked it down to a Code 3 of 9 (Code 39) barcode generator written by Robert de Bath and published on MiBuso, a well-known NAV/Business Central community site.
The code is 11 years old, written for an older version of NAV, and exported in the classic NAV text format. It includes both a report and a codeunit, but Erik focuses solely on the codeunit since converting reports is a much more involved process.
Step 1: Convert the Text Objects to AL Using txt2al
Microsoft provides a tool called txt2al that ships with the Business Central version 14 installation image. This tool takes NAV objects in .txt format and converts them into .al files.
The usage is straightforward:
txt2al --source=. --target=al
This reads the NAV text file from the current directory and outputs the converted AL files into an al subfolder. The result is a .al file for the barcode codeunit and an accompanying translation file.
Step 2: Import into a Business Central Extension Project
Before recording the video, Erik had already set up a minimal AL extension project with:
- A table with a barcode text field and a blob field for the barcode picture
- A page to display those fields
He copied the generated .al file into the project, and immediately the compiler flagged issues.
Step 3: Fix the Object ID
The original codeunit had an object ID of 99944, which is outside the allowed range for extensions. Erik simply changed it to fit within the extension’s ID range (in this case, 50300). The project’s app.json defines the valid range:
{
"idRanges": [
{
"from": 50100,
"to": 50149
}
]
}
Step 4: Remove the Legacy Blob Table Reference
The original code took a Record parameter referencing the Object table (table 2000000001) — a hack used to pass a blob field before the TempBlob construct existed in NAV. Erik modernized this by:
- Removing the Record parameter entirely
- Promoting the internal
OutStreamvariable to a function parameter instead - Removing the now-unnecessary stream creation and memory allocation code inside the codeunit
This is a much cleaner approach: the caller creates the stream and passes it in, and the barcode codeunit writes directly to it.
Step 5: Replace Char with Byte
This is the most important technical change. Old NAV ran in ASCII/codepage mode with an 8-bit character set. Business Central runs in Unicode with a 16-bit character set. The original code used the Char data type extensively to manipulate individual pixel values in the bitmap.
In Unicode mode, Char is 16 bits wide, which breaks the bitmap byte layout. The fix is a simple find-and-replace:
// Replace all instances of:
Char
// With:
Byte
The Byte type is a true 8-bit construct in AL, which is exactly what’s needed for bitmap pixel manipulation. Erik demonstrated what happens if you leave Char in place — the barcode renders, but the background turns gray instead of white. This is because a white pixel value of 255 gets misinterpreted when stored in a 16-bit char instead of an 8-bit byte.
Step 6: Wire It Up on the Page
On the test page, Erik added an action that calls the barcode codeunit. The key steps in the action trigger are:
- CalcFields on the blob field (required when working with blob fields)
- Create an OutStream from the blob field
- Configure the barcode codeunit (set DPI, quiet zone, etc.)
- Call MakeBarcode, passing the barcode text and the OutStream
- Modify the record to save the generated bitmap
The barcode codeunit’s configuration included setting values like DPI to 48, a width of 9600, and a height around 800-900, with the quiet zone enabled.
The Result
After publishing the extension, Erik created records with different barcode values (e.g., “1000” and “ERIK”) and the codeunit generated distinct, valid Code 39 barcode bitmaps directly in the blob field — all rendered purely in AL code with no external dependencies.
Summary and Key Takeaways
- txt2al is a useful tool from the BC 14 installation image that converts NAV text-format objects into AL files
- Not all objects convert cleanly — codeunits tend to be easiest since they’re pure logic. Tables with FlowFields, reports, and other objects may need more cleanup
- Char → Byte is a critical change when porting code that deals with binary data. Old NAV’s 8-bit
Chardoesn’t behave the same as Business Central’s 16-bit UnicodeChar - Modernize blob handling — replace old Record-based blob hacks with OutStream parameters for cleaner, more idiomatic AL code
- MiBuso is a treasure chest of NAV and Business Central knowledge — both legacy code like this and modern Business Central content
This example shows that with just a few minutes of work, legacy NAV code that’s over a decade old can be brought into the modern Business Central world and continue to provide value.