top of page
  • marco ferrari

Namespace in AL: use and (mal)functioning

If you donwloaded the new BC October release, you probably noted that all the standard objects contain the namespace declaration. Below you can see the Production Order table, with namespace Microsoft.Manufacturing.Document.

The instruction that declares the belonging of a certain object to a specific namespace then provides that wherever we decide to reference that object, it will be necessary to previously declare the namespace to which it belongs through the using instruction.

Table Production Order above, for example, uses all the namespaces declared with the using instructions.

Use of namespaces

The first question now is: are we forced to use namespaces in our extension objects?

The answer is no, we don't have to, at least for now. But their use can be useful.

Namespaces essentially are used to classify the objects of an extension, then allowing, through the AL explorer, to filter and see them grouped by namespace, as shown below.

Microsoft introduced many namespaces, but fortunately we don't have to know them when we devolp, because Visual Studio Code helps us with this.

The definition of a namespace is very simple: when we create a new object we must first of all delcare the namespace by using the namespace instruction followed by the name. If we have already defined a namespace, the intellisense will suggest it to us, otherwise it will simply create a new one.

Namespaces should be hierarchical, starting with the extension name followed by a group or groups separated by a dot. Please note that our namespaces must to start with the word Microsoft.

When we introduce namespaces in our extensions, Visual Studio Code automatically adds the using statements when we declare variables, set the TableRelation property or insert parameters to our procedures that refer to standard objects (or ours).

Below you see an example of a custom table which starts with the declaration of the namespace and within which the TableRelation property has been set in a field connecting it to the standard No. Series table. Having defined our own namespace for the object, Visual Studio Code automatically adds the using statement at the top of the object, thus telling the system that our table will use the objects belonging to the Microsoft namespace. Foundation.NoSeries.

But this not always happens.

A small bothersome bug

There are some situations where these are not added, with the result that the system reports several syntax errors.

In the example below, I have created a codeunit where I subscribe an event of the Item Journal Line table. While when defining the membership table (Database::"Item Journal Line") Visual Studio Code correctly adds the using Microsoft.Inventory.Journal statement, it does not do so when I add the SalesLine parameter of the procedure. The result is that my code seems to be incorrect.

Now, if one doesn't know the namespace of the object, the only solution I found was to declare a variable that reads it, the declaration of which causes Visual Studio Code to add the necessary using statement

Now the variable can be deleted, as it's useless.

108 views0 comments


bottom of page