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)
var
    StartingDate: Date;
begin
    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
        DateToken.Contains('monday'):
            FromDate := CalcDate('<-CW>', StartingDate);
        DateToken.Contains('tuesday'):
            FromDate := CalcDate('<-CW+1D>', StartingDate);
        DateToken.Contains('wednesday'):
            FromDate := CalcDate('<-CW+2D>', StartingDate);
        DateToken.Contains('thursday'):
            FromDate := CalcDate('<-CW+3D>', StartingDate);
        DateToken.Contains('friday'):
            FromDate := CalcDate('<-CW+4D>', StartingDate);
        DateToken.Contains('saturday'):
            FromDate := CalcDate('<-CW+5D>', StartingDate);
        DateToken.Contains('sunday'):
            FromDate := CalcDate('<-CW+6D>', StartingDate);
        else begin
                Handled := false;
                exit;
            end;
    end;
    ToDate := FromDate;
    Handled := true;
end;

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

Recent Articles from the category

Isolated events

Isolated events

With Business Central 2022 wave 1, a new setting for event publishers was introduced. Until this version, any error in any event subscriber caused interruption to the current running process and stopped the whole activity. In some cases (such as log-in), this is...

read more
How to let users choose field(s) properly

How to let users choose field(s) properly

When some complex functionality is developed, it is sometimes necessary to let users choose a specific field. This can be required for field permissions, mapping imported values or any similar process. Earlier, the usual way was to create a link on the Field table...

read more
Working with Translation Files (XLIFF)

Working with Translation Files (XLIFF)

How does the translation file work? In Microsoft Dynamic 365 Business Central, displaying different languages is done using the XLIFF files extension (.xlf). The generated XLIFF files have strings that are specified in properties like Captions, Tooltips......

read more
1D & 2D Barcodes in Business Central 19.1

1D & 2D Barcodes in Business Central 19.1

Business Central (cloud-only!) includes one-dimensional (1D) barcode fonts since Microsoft Dynamics 365 Business Central 2020 wave 2 (17.0) was released in October 2020. It is a great improvement to how developers can handle requirements from their clients to print...

read more
ErrorInfo data type & Collectible Errors

ErrorInfo data type & Collectible Errors

A major change to AL Language development is here! Collectible errors can hugely improve any validation procedures. And what's new? Everything is about a new data type ErrorInfo. ErrorInfo data type ErrorInfo is a new data type introduced in Microsoft Dynamics 365...

read more
Exploring CosmosDB with Business Central, part 2

Exploring CosmosDB with Business Central, part 2

This part explains how to connect to Azure Cosmos DB from your Business Central extension using AL Language and how to get some values from the database.------------------------------------------------------------------------------------------------------------------...

read more
DynamicsCon 2021 session recording available!

DynamicsCon 2021 session recording available!

Finally, recordings from DynamicsCon 2021 Autumn edition are available! Check the official youtube channel. It was a great conference with over 250 people during my live session! I definitely hope I will be able to attend the next in-person DynamicsCon as a speaker...

read more
Directions EMEA

Directions EMEA

Directions4Partners EMEA 2021 is still in progress here in Milan. I want to share all details about each day, but unfortunately, there are so many amazing things to see, watch and try I have almost no time. So today just a few words about the day before the...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here