Business Central 2024 wave 2 (BC25): New && filter syntax in the SetFilter() method

Dynamics 365 Business Central

Hi, Readers.
Dynamics 365 Business Central 2024 wave 2 (BC25) is generally available. More details: General availability: Dynamics 365 Business Central 2024 release wave 2 (BC25).

I will continue to test and share some new features that I hope will be helpful.

Specify and use full-text search indexes on table fields:

Business value:
With the new full-text search metadata on table fields, AL developers can significantly enhance data search features, making them easier and faster for users. This feature improves user satisfaction and productivity by providing more precise and relevant search results.

Feature details:
As an AL developer, you can now specify which table fields are included in full-text search indexes on the database. By doing this, the following search features will be faster and provide better search capabilities:

  • Companywide search (data search)
  • List page search
  • Lookup search
https://learn.microsoft.com/en-us/dynamics365/release-plan/2024wave2/smb/dynamics365-business-central/specify-use-full-text-search-indexes-table-fields?wt.mc_id=DX-MVP-5004336

To specify full-text search, you set the OptimizeForTextSearch property on fields in tables or table extensions. We have discussed this before. More details: Business Central 2024 wave 2 (BC25): New options for Search in lists (Modern Search and new OptimizeForTextSearch property)

This time I want to discuss a new point. Modern Search can also be defined in AL code.
In AL code, you can also use a new && filter syntax in the SetFilter() method.

FieldRef.SetFilter('&&' + SearchString + '*')

As for searching text, let’s look at a few examples. Page Inspection -> Page Filters:
london -> &&london*

london chair -> &&london*&&&chair*

london chair blue -> &&london*&&&chair*&&&blue*

So each word is a ‘&&’ + SearchString + ‘*’, with & used to connect them. If you know this, you can use it in your code. A very simple example: Record.SetFilter(Any, Text [, Any,…]) Method

In filter pane:

Another simple example: FieldRef.SetFilter(Text [, Any,…]) Method

Test code:

pageextension 50123 ItemListExt extends "Item List"
{
    trigger OnOpenPage()
    var
        RecRef: RecordRef;
        FldRef: FieldRef;
    begin
        RecRef.Open(Database::Item);
        FldRef := RecRef.Field(3);
        if FldRef.IsOptimizedForTextSearch then
            FldRef.SetFilter('&&London*&&&chair*&&&blue*');
        if RecRef.FindSet() then
            Message(FldRef.Value);
        //Rec.SetFilter(Description, '&&London*&&&chair*&&&blue*');
    end;
}

You can find standard examples in codeunit 2680 “Data Search in Table” -> local procedure SetListedFieldFiltersOnRecRef

PS: This codeunit is in the Data Search extension

And if the search text contains multiple words, you will not be able to search for results if you do not split them. (This is why I failed the test before😑)

You can create a generic function to handle this, it’s not very hard. For example,

Source code:

pageextension 50123 ItemListExt extends "Item List"
{
    trigger OnOpenPage()
    var
        SearchString: Text[250];
        RecRef: RecordRef;
        FldRef: FieldRef;
    begin
        SearchString := 'London chair blue';
        Rec.SetFilter(Description, SplitSearchString(SearchString));
    end;

    local procedure SplitSearchString(var SearchString: Text[250]): Text[250]
    var
        i: Integer;
        StringList: List of [Text];
    begin
        StringList := SearchString.Split(' ');
        if StringList.Count > 0 then
            for i := 1 to StringList.Count do begin
                if i = 1 then
                    SearchString := '&&' + StringList.Get(i) + '*'
                else
                    SearchString := SearchString + '&&&' + StringList.Get(i) + '*';
            end;
        exit(SearchString);
    end;
}

Great, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL