top of page
Search
  • marco ferrari

Creating user snippets in AL. A simple example

During AL programming we frequently need smilar objects and structures. A typical example is a table made by two fields: a code of ten characters and a text of 50. I probably created this table hundrends of time.


With the introduction of the AL language and Visual Studio Code, today is possible to use not only the standard snippets for new objects (eg. ttable for new table, tpage for a new page etc), but we can also create personal snippets.


Let’s now see how to create a snippet to create this frequent table.


User snippets

In order to create new snippets in Visual Studio Code, we have to click con the settings buttons, User Snippets.

This menu opens the editor of the al.jason file, in which we can write our snippets.

By reading the comments we can understand the syntax to be used, that in our case will be

The instruction “prefix” defines the string that activates the snippet, while in the “body” one we write the code. The string \t is simply the TAB to indent our code.


Please note that spaces between \t are not necessary, I just used them to make the code more readable, so the code should written in this way

Now, let’s create a new .al file and start to digit the “prefix”. What we see is our new snippet

with the final result

The code must finally be completed with the ID and the name of the table.


Variables

In the snippet definition it's also possible to define some variables to be specified by the user after calling the snippet itself. For example, suppose you want the user to specify the name of the first field instead of using Code. We should write our snippet like this:

The placeholder ${1:FirstField} defines a variable on which the user will be placed by pressing the TAB button after calling the snippet

This placeholder can be called in multiple places, so for example, we can call it even in the key definition

so that when the user specifies the name of the field, this name will be inserted in the key definition.


Placeholders can be more than one, the integer number appearing as the first character in its definition is simply the order in which the user will be placed by pressing TAB.


Conclusions

With the user snippets we can really speed up our daily work of development, by setting in advance properties that we usually forget or having a complex syntax, or we can even create standard complex objects containing lines of code already prepared.


With respect to an initial boring work of writing snippets, we can save a lot of time.

117 views0 comments
bottom of page