If you’re anything like me, when you’re developing solutions in AL you often want to refer to the base BC app and see how something works.
This almost always starts with navigating to a specific, known object and then using a series of ‘Go to Definition’ and ‘Find All References’ commands, with liberal use of the ‘Alt + Left’ key combination to trace your way back when you realise you’ve gone the wrong way.
However, finding that initial starting point can be a real pain – for example, if I want to use the global search function to find the item record I get this:
It’s a list of every time the word ‘Item’ is used, regardless of context, and results in more than 10k responses – too many to be useful.
I could of course write ‘Table 27 ‘ and it would return one result, the item table object. But even in the days of C/AL I never tried too hard to learn object IDs, I’m even less interested in knowing them now.
What I really want is to be able to perform a search for an object name, and only return objects.
What I really want is this:
This also works for all objects, such as the sales header:
Or maybe I’d like the sales order page:
So, how is this achieved? The answer is in the black magic that is Regular Expressions, or regex. And it looks a little like this:
\w+ \d+ "*
Regex can be quite intimidating the untrained eye. I won’t try and explain it all, as folk much better than I have already done so. Effectively, regex is a method which allows us to define a pattern and return the results of that pattern. It’s uses are numerous, and once you get a grasp of the concept, it’s uses are endless.
For reference I include a link to the Wikipedia entry , a basic guide on how to use regular expressions, and most importantly my #1 favourite tool for learning/trying out my regex.
For our purposes today, there are only a few, simple terms required.
Firstly, in the search box of VS Code make sure you’ve selected the ‘Use Regular Expressions’ option
There are then three key components to this search:
- \w+ (Search for any word)
- \d+ (Search for any number)
- “* (Search regardless of whether there is a double quotation mark)
Objects in business central all* start out with three pieces of information:
- The object type
- The object ID
- The object name – which if including a space or special characters is surrounded by double quotation marks
*Before any pedants chip in – This doesn’t include the new ‘interface’ objects – they don’t get object IDs. But for now, this method is sufficing for me
I’m sure most of you at this point can guess where this is going.
The name of an object is a pattern, and we’ve got a regex which matches that pattern. Effectively we’re asking for: AnyWord_Space_AnyNumber_Space_MaybeADoubleQuote_TheObjectName
The catch all search is:
\w+ \d+ "*
If we wanted to search just within a specific set of object types, we can replace \w+ with the object type. eg:
codeunit \d+ "*
- \w is the regex term to match a letter
- \d is the term to match a number
- + is the term to match multiple of the preceding pattern
- * is the term to mean ‘0 or more’
Therefore \w+ says match any number of alphabetical characters, \d+ says to match any number of digits, and “* says to match 0 or more double quotation marks.
The space characters between the terms are part of the pattern – there must be a space between the word and the number, and there must be a space between the number and the maybe quotation mark.
The method isn’t perfect, and maybe better options exist.
For example, I suspect it could be expanded to use regex groups to match the term within an object name (such as the search for item also matching codeunit 1327 “Adjust Item Inventory”. However, I’ve not found enough need to spend time on working on it. If you do find ways to improve this, then please do let me know!
However, I find that for me personally this has made my navigation of the base app much easier.
And also, it’s made me that bit more familiar with regex – never a bad thing!