How to add Custom Filter Tokens in Business Central

Updated: Aug 4, 2020

Custom Filter Tokens is an interesting functionality in Business Central. Many of us do not know about this functionality. Using this functionality, you can add Filter Tokens like Tomorrow, Yesterday, ThisMonth, PrevMonth, FiscalYear, PrevFiscalYear, NextFiscalYear etc. so that User can use them just like Today and WorkDate Filter Tokens to apply date filters.


You can add Filter Tokens for Date, Time, DateTime and Text data types.


Filter Tokens [Codeunit 41]


This exposes functionality that allow users to specify pre-defined filter tokens that get converted to the correct values for various data types when filtering records.


It has the following events you can subscribe:

  • OnResolveDateFilterToken

  • OnResolveDateTokenFromDateTimeFilter

  • OnResolveTextFilterToken

  • OnResolveTimeFilterToken

  • OnResolveTimeTokenFromDateTimeFilter

Sample Code


For demonstration purposes, we are using OnResolveDateFilterToken event to add Tomorrow, Yesterday, ThisMonth, PrevMonth, FiscalYear, PrevFiscalYear and NextFiscalYear tokens.


Date Filter Tokens Impl [DateFilterTokensImpl.Codeunit.al]

codeunit 50103 "Date Filter Tokens Impl"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Filter Tokens", 'OnResolveTimeTokenFromDateTimeFilter', '', false, false)]
    local procedure OnResolveDateFilterToken(DateToken: Text; var FromDate: Date; var ToDate: Date; var Handled: Boolean)
    begin
        case UpperCase(DateToken) of
            'YESTERDAY', 'YD':
                begin
                    FromDate := CalcDate('-1D', Today);
                    ToDate := FromDate;
                    Handled := true;
                end;
            'TOMORROW', 'TO':
                begin
                    FromDate := CalcDate('-1D', Today);
                    ToDate := FromDate;
                    Handled := true;
                end;
            'THISMONTH', 'TM':
                begin
                    FromDate := CalcDate('CM - 1M + 1D', Today);
                    ToDate := CalcDate('CM', Today);
                    Handled := true;
                end;
            'PREVMONTH', 'PM':
                begin
                    FromDate := CalcDate('CM - 2M + 1D', Today);
                    ToDate := CalcDate('CM - 1M', Today);
                    Handled := true;
                end;
            'FISCALYEAR', 'FY':
                begin
                    FromDate := GetFiscalYear();
                    ToDate := CalcDate('+12M - 1D', FromDate);
                    Handled := true;
                end;
            'PREVFISCALYEAR', 'PFY':
                begin
                    FromDate := CalcDate('-1Y', GetFiscalYear());
                    ToDate := CalcDate('+12M - 1D', FromDate);
                    Handled := true;
                end;
            'NEXTFISCALYEAR', 'NFY':
                begin
                    FromDate := CalcDate('+1Y', GetFiscalYear());
                    ToDate := CalcDate('+12M - 1D', FromDate);
                    Handled := true;
                end;
        end;
    end;

    local procedure GetFiscalYear(): Date
    var
        AccountingPeriod: Record "Accounting Period";
    begin
        AccountingPeriod.Reset();
        AccountingPeriod.SetRange("New Fiscal Year", true);
        AccountingPeriod.SetRange(Closed, false);
        if AccountingPeriod.FindFirst() then
            exit(AccountingPeriod."Starting Date");
    end;
}


Filter Tokens in Action:



Conclusion:

Users will definitely like this functionality because you are saving lot of his / her time. For example, If User can enter FiscalYear as Date Filter instead of recollecting the start and the end date of the fiscal year and typing it manually, he can save a lot of his time.

You can download the source code from GitHub.


#MsDyn365 #MsDyn365BC #BusinessCentral

621 views0 comments

© 2020 by msnJournals.com

  • Facebook Social Icon
  • LinkedIn Social Icon
  • Twitter Social Icon
  • GitHub-Mark-120px-plus