Custom Filter Tokens

by May 3, 2022AL Language

Home 9 Development 9 AL Language 9 Custom Filter Tokens

As a user of the Business Central you have some constants you can use to filter or insert values. These constants contain useful values for data manipulation such as

  • t / today for date field – return current date
  • q / quarter for date field filters – return range of the current quarter
  • w / week, m / month, y / year ….

However, in some cases, we need some other constants. For that reason, we can use codeunit 41 “Filter Tokens” (codeunit 58 “Filter Tokens Impl.”). It contains functionality that allows defining our own constants for custom fields. Source codes for this functionality are on Microsoft’s GitHub.

Let’s look at some details.

Available procedures & events in “Filter Tokens” codeunit

We have four different procedures for filter token validation based on the requested data type. All procedures accept filter token as a parameter and returns (within the same input-output parameter) date value or date filter value.

procedure MakeDateFilter(var DateFilterText: Text)
procedure MakeTimeFilter(var TimeFilterText: Text)
procedure MakeTextFilter(var TextFilter: Text)
procedure MakeDateTimeFilter(var DateTimeFilterText: Text)

These procedures have support for some constants by default.

For text variables, supported constants are ME, USER (return UserId()) and COMPANY (returns CompanyName()).

For date, time and DateTime variables we have similar constants to the constants available for standard table filters such as NOW, TODAY, WORKDATE, YESTERDAY, TOMORROW, WEEK, MONTH, YEAR.

However, the biggest advantage of this functionality is that we can use our own constants. To add support for a new constant we have to define constant transformation (to date or time variables) using the following OnResolve publishers.

procedure OnResolveDateFilterToken(DateToken: Text; var FromDate: Date; var ToDate: Date; var Handled: Boolean)
procedure OnResolveTextFilterToken(TextToken: Text; var TextFilter: Text; var Handled: Boolean)
procedure OnResolveTimeFilterToken(TimeToken: Text; var TimeFilter: Time; var Handled: Boolean)
procedure OnResolveDateTokenFromDateTimeFilter(DateToken: Text; var DateFilter: Date; var Handled: Boolean)
procedure OnResolveTimeTokenFromDateTimeFilter(TimeToken: Text; var TimeFilter: Time; var Handled: Boolean)

How to add own constants

Imagine we want to add support for new constants Monday to Sunday from Last, This and Next week. To do that, just implement OnResolveDateFilterToken from Codeunit Filter Tokens. See code below.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Filter Tokens", 'OnResolveDateFilterToken', '', false, false)]
local procedure OnResolveDateFilterTokenFilterTokens(DateToken: Text; var FromDate: Date; var ToDate: Date; var Handled: Boolean)
    StartingDate: Date;
    StartingDate := WorkDate();
    if DateToken.Contains('last') then
        StartingDate := CalcDate('<-CW-1D>', WorkDate());
    if DateToken.Contains('this') then
        StartingDate := CalcDate('<-CW>', WorkDate());
    if DateToken.Contains('next') then
        StartingDate := CalcDate('<+CW+1D>', WorkDate());

    case true of
            FromDate := CalcDate('<-CW>', StartingDate);
            FromDate := CalcDate('<-CW+1D>', StartingDate);
            FromDate := CalcDate('<-CW+2D>', StartingDate);
            FromDate := CalcDate('<-CW+3D>', StartingDate);
            FromDate := CalcDate('<-CW+4D>', StartingDate);
            FromDate := CalcDate('<-CW+5D>', StartingDate);
            FromDate := CalcDate('<-CW+6D>', StartingDate);
        else begin
                Handled := false;
    ToDate := FromDate;
    Handled := true;

That’s all. We can try it for example in sales orders. Using “ friday” we will get all orders with a posting date between this Friday and the next one (inclusive).

Recent Articles from the category

Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more
Clean up your copied environments

Clean up your copied environments

One of the most important things every developer should handle is to clean up the environment when the environment (or a company) is copied. Especially if the environment is managed directly by a client and they can create new copies anytime. Similarly, for copied...

read more
7 git commands you should know

7 git commands you should know

For many years, developers in C/AL did not need to know anything about Git or other versioning tools. That has changed with Business Central, or more specifically with AL Language. Today, we will look at the most important (and basic) git commands any developer should...

read more
How to define the source for item reservations?

How to define the source for item reservations?

It's not uncommon to have a customer's request to limit from which source items could be reserved. For example, customers may not want to reserve items from return orders. How can we achieve this goal? It's really simple. All the magic is done in the procedure...

read more
NavigationAction for ErrorInfo data type

NavigationAction for ErrorInfo data type

One more article about ErrorInfo data type. Have you already read my previous posts about ErrorInfo and Collectible Errors? ErrorInfo data type & Collectible Errors | MSDyn365 Business Central - Ing. Tomáš Kapitán ( Collectible Errors?! | MSDyn365...

read more

Sign Up for News


Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here