Make your own Date Filters

In Business Central, you can create your own fancy date filter codes. In this video, I show how easy it is to create filter codes to set filters based on how your business is running.

https://youtu.be/-nAVYuiOm5k

In this video, Erik demonstrates how to create custom date filter tokens in Business Central using AL. Instead of building custom actions or buttons to set date filters, you can hook into the built-in filter token system so users can type custom keywords directly into any date filter field — just like the built-in tokens such as week, today, or tomorrow.

Built-in Date Filter Tokens

Business Central already provides several powerful date filter tokens out of the box. When you’re filtering on a date field — such as Posting Date on General Ledger Entries — you can type keywords instead of actual dates:

  • tomorrow — filters to tomorrow’s date
  • w — filters to the current work date
  • tuesday — filters to the Tuesday relative to the work date
  • week — filters to the current week
  • current month — filters to the current month
  • Numeric ranges like 3..5 — resolved relative to the work date

These are incredibly useful for power users, but sometimes customers need custom tokens tailored to their business — for example, filtering to a specific event date, a schedule, or a dynamically calculated range.

Understanding Filter Tokens

The concept of “tokens” here is the same as in programming language parsing. When you type a filter expression, Business Central breaks it down into individual tokens. For example, typing 3..5 produces three tokens: 3, .., and 5. Each token is then resolved — and for date fields, the system attempts to interpret text tokens as dates.

The key insight is that when the system encounters a token it doesn’t recognize, it fires an event before it fails. This gives your extension the opportunity to handle the token and return the appropriate date values.

Subscribing to the OnResolveDateFilterToken Event

The event we need lives in the Filter Tokens codeunit. Specifically, we subscribe to OnResolveDateFilterToken. This event provides:

  • DateToken (Text) — the raw text the user typed
  • FromDate (Date) — the start date you can set
  • ToDate (Date) — the end date you can set
  • Handled (Boolean) — set to true if you’ve resolved the token

Here’s the complete source code for a custom date filter token called “party”:

codeunit 50100 "DateFilters"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Filter Tokens", 'OnResolveDateFilterToken', '', true, true)]
    local procedure MyFilters(DateToken: Text; var FromDate: Date; var ToDate: Date; var Handled: Boolean)
    begin
        if DateToken.ToLower() = 'party' then begin
            FromDate := Today() + 10;
            ToDate := FromDate;
            Handled := true;
        end;
    end;
}

A few important details about this implementation:

  • We use DateToken.ToLower() to make the comparison case-insensitive, so users can type “Party”, “PARTY”, or “party” and it will all work.
  • Setting FromDate and ToDate to the same value results in a single-date filter. If they differ, Business Central displays it as a date range.
  • Setting Handled := true tells the system that we’ve resolved this token — without it, the system will treat the token as unrecognized and show an error.

How It Behaves at Runtime

Erik walks through the debugging experience to show exactly what happens. When you type “party” into a date filter field and tab out:

  1. The event fires with DateToken = 'party', FromDate and ToDate both empty, and Handled = false.
  2. Your code matches the token, sets the dates, and marks it as handled.
  3. Business Central applies the resolved dates as the filter.

If you type something the system doesn’t recognize and your code doesn’t handle either — like “cleanup” — the event fires but since no subscriber sets Handled to true, the system reports an invalid filter. Interestingly, Erik notes that unrecognized tokens may cause the event to fire more than once, but that’s perfectly fine as long as your logic is deterministic.

Combining Custom Tokens with Other Filters

Custom tokens work seamlessly with standard filter syntax. You can type expressions like today..party and Business Central will resolve each token independently — today through the built-in logic and party through your event subscriber — then combine them into a proper date range filter.

Saved Views and Dynamic Filters

One particularly interesting behavior Erik highlights is how saved views interact with custom tokens. When a user saves a view that includes a custom token filter like today..party, Business Central does not save the resolved dates. Instead, it saves the raw filter text. This means:

  • The filter is re-evaluated every time the view is loaded.
  • If your token logic is dynamic (e.g., based on Today()), the filter will produce different results on different days.
  • This makes custom tokens ideal for dynamic, rolling date filters.

This is a significant advantage over an action-based approach where you’d set a fixed date filter — saved views with custom tokens remain dynamic.

Other Filter Token Events

The Filter Tokens codeunit exposes several similar events beyond date filters:

  • OnResolveDateFilterToken — for Date fields (the one demonstrated)
  • OnResolveDateTimeFilterToken — for DateTime fields
  • OnResolveTimeFilterToken — for Time fields
  • OnResolveTextFilterToken — for Text fields

The same principle applies to all of them: subscribe to the event, check the token, set the appropriate output values, and mark it as handled.

Practical Use Cases

While the “party” example is deliberately simple, Erik points out that the real power comes from dynamic, data-driven tokens. Since the event subscriber is just AL code, you can:

  • Look up schedules or planned activities and filter to the last planned date
  • Calculate business-specific date ranges like fiscal periods or contract terms
  • Reference external data to determine meaningful date boundaries
  • Create industry-specific shorthand that your users will find intuitive

Conclusion

Custom date filter tokens are an elegant alternative to building custom actions for setting date filters. By subscribing to the OnResolveDateFilterToken event in the Filter Tokens codeunit, you let users type custom keywords directly into any date filter field — maintaining the native filtering experience while adding business-specific functionality. The tokens integrate naturally with Business Central’s filter syntax, work with saved views (and remain dynamic when they do), and require minimal code to implement. It’s a power-user feature that, once discovered, can significantly streamline how users interact with filtered lists.