Searching the Business Central Base App with Regular Expressions

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:undefined
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:
undefined

This also works for all objects, such as the sales header:
undefined

Or maybe I’d like the sales order page:
undefined

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!

7 thoughts on “Searching the Business Central Base App with Regular Expressions

  1. Natalie

    Since RegEx is still a mystery to me, I really appreciate your introduction to it. Thank you!

    However, regarding your examples, the same could have been simply achieved by using Ctrl + P and then typing the object name + type parts.

    Looking at your gifs (always Ctrl + P + …):
    1. Item
    2. Sales Header or SalesHeader
    3. Sales Order or SalesOrder

    If you want to search for a specific object type, simply add it after the object name, e.g. for the Item table:
    – Item Table or ItemTable

    Like

    1. Thanks Natalie, I wasn’t aware of ctrl+p in vs code at all. It’s a great feature! AJ Kaufman also pointed it out, but on twitter, and he also made mention that ctrl+p allows for partial matches! Such as ‘salcodpos’ matches the sales post codeunit.

      Like

  2. Pingback: New Command in My CRS AL Language Extension: Search Object Names

  3. Pingback: New Command in My CRS AL Language Extension: Search Object Names - Dynamics 365 Business Central Community

  4. Pingback: New Command in My CRS AL Language Extension: Search Object Names - 365 Community

  5. Pingback: New Command in My CRS AL Language Extension: Search Object Names - Waldo's Blog - Dynamics 365 Business Central/NAV User Group - Dynamics User Group

  6. Preben Rasmussen

    Try these…

    Search for tooltip without trailing dot:
    ToolTip = ‘.+(\w\W\W)$

    Search for some (wrong) casing terms as remnants of txt2al or mistyping:
    (IF)|(THEN)|(BEGIN)|(END)|(ELSE)|(FOR\s)|(\sDO\s)|(FORMAT)|(REPEAT)|(UNTIL)|(FINDSET)|(FINDFIRST)|(FINDSET)|(FIND)|(EXIT)|(WHILE)|(INSERT)|(MODIFY)|(DELETE)|(RENAME)|(VAR)|(OF)|(Exit)|(\sArray\s)|(\slist\s)|(\sTemporary)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.