Snippets: From zero to quick creation tips. Patterns
Views (415)
Introduction
Snippets are pieces of re-usable code. We can compare them to templates, but they have specific features that make them stronger other code re-usage systems:
. Fast triggering. You already know what I mean: you place the cursor in editor and type ttable+Tab and voila, the magic happens and all the AL table code is in your screen.
. Placeholders. Are tab stops that lead the user to all the variable that you must replace in the code that the snippet brings you. The placeholders give developers fast guidance to pattern use. You don´t have to look for substitutions only press tab and replace, press tab and replace until you complete the implementation.
Our own snippets quickly
To make a snippet we need two steps.
First step, create or open a user snippet: F1 to command line and type “>configure User snippets”, and then you can choose to create a snippet or edit an existing one:
Second write our code in this json format:
{
"AL label variable definition": {
"scope": "al",
"prefix": "tlabel",
"body": [
"$LabelName: Label '$LabelText';"
],
"description": "AL label variable declaration"
}
}
For a better explanation about these elements you can see this link: https://vscode-docs.readthedocs.io/en/stable/customization/userdefinedsnippets/#creating-your-own-snippets
I only want to highlight what a few things:
- Scope is the language where a snippet will be triggered. Not supported in al snippet files. This statement is not allowed in al.json file, as far I know is only for .code-snippets files.
- Prefix. Very important, is the text you type to find quickly the snippet, so you must choose this text carefully. In al language begin with “t”, ttablext, tpageext, tpagecard and so on.
- Body. The real deal, the re-usable code. After you copy here the code, you must put double colon at the end and start of each code line, scape special characters with bar, and separate all the lines with comma. But you don´t have to do it manually. Later will see tools to avoid this strong pain.
- Tabstops. Are the words with “$” prefix. This way we define the quick substitution.
For ease the conversion from code to snippet format the best choice I found is this link:
You paste your code in left panel, and you get it converted in snippet format in the left:
In previous posts I wrote about creating our own snippets but with a lot of pain: In the first one I made a snippet and coded it manually, and the second post I recommended a good tool, but without maintenance and support.
Snippets sharing methods
There are many ways to share snippets. The worst way is the one I recommended two years ago, edit the al.json and paste our new snippet there.
The best is package it in a vsix file to distribute.
An intermediate solution for sharing is create a new snippet as I did it above (F1 and type “>configure User snippets”), and create a new file with “code-snippets” file extension. For use the snippets in another machine you only need to paste these files in %APPDATA%\Code\User\snippets folder.
Patterns and pattern lab
In my opinion the best way to use patterns AL is turning them in snippets.
If you want to see an example take from Mark Brummel pattern repository a pattern and turn it in snippet with the techniques of this post.
I took Singleton pattern code:
and I turned it on a snippet. You have the result in the link bellow:
After you make the snippet pattern you can use it to check how fast and simple you can use the patterns this way.
Comments
-
All AL developers are using snippets, unless you feel an inexplicable urge to write the same code patterns over and over again. If you want a good definition of snippet, here you are: “Code snip...

Like
Report
*This post is locked for comments