#HumpDayHacks with Tonya Bricco-Meske

#HumpDayHacks: Stop Hardcoding Object IDs in Business Central AL Code

When I started this blog, I wanted to create a reference for new developers learning the basics of AL development. But I’ll be honest: even experienced developers (and AI) can make mistakes or miss best practices.

Over the past month, one of the projects I manage has gone through a complete app restructure. As we add localizations, our app dependencies are changing, and we are moving code between new custom apps. During that process, I started worrying about code that referenced objects by ID.

Let’s say we have report 50100 MyReport, and we move it to a new app where it becomes report 60000 MyReport. What happens if a custom action was written like this?

The compiler is not going to tell us that the report ID changed. Because we own the report, we could search for 50100 and update every reference. But who wants to sort through every field, object, and unrelated value that happens to use the same number just to make sure nothing breaks?

This is why it is important to reference objects by name whenever AL allows it.

The Better Option

Both examples point to the same report, but the second version communicates the developer’s intent and gives the compiler and development environment more useful information. It replaces a hardcoded object ID, oftentimes called a magic number, with a strongly typed object reference.

Compile-Time Validation

The most important advantage of Report::MyReport is that the compiler can validate the reference.

If MyReport is renamed or deleted, the compiler can flag the reference so you can fix it before the extension is deployed. With a hardcoded value such as 50100, the number can remain in the code even after the object it originally represented has changed or no longer exists. The problem may not appear until the code runs.

Leveraging named references moves the feedback for the developer earlier in the development process – instead of runtime validation, we get that from the compiler.

Better Navigation in VS Code

When you use Report::MyReport, you can navigate to the object definition with F12 (Go to Definition). You can also use Shift+F12 (Find All References) to see where the object is used.

A raw integer does not provide the same connection. Visual Studio Code sees the number, but the number does not carry the symbolic relationship that enables object navigation.

Final Takeaway

Hardcoded object IDs may be quick to type, but they make code harder to understand and easier to break. Report::MyReport turns an unexplained number into an explicit object reference that the compiler, VS Code, and other developers can understand. The named version gives you

  • compiler-time validation of the object reference
  • code that clearly identifies the object being used
  • Safer renumbering and refactoring
  • Go-to Definition and Find All References support
  • Less reliance on developers memorizing Object IDs

The next time you are about to enter an object ID directly into an AL method call, pause and ask whether you can reference the object by type and name instead. Replacing a magic number with a named object reference is a small change that makes Business Central code safer, clearer, and easier to maintain.

Leave a Reply