Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Implementing an Items search engine with Business central API

Posted on by 2,983

We are trying to implement a Item search functionality using the Business Central API.

For example, the user may type "PLATE" in the search box, and we want to search by Item No. and Description; so we take this to the API with a request like this:

GET https://

SERVER:7048/BC210/api/v1.0/companies(5b9a4086-229b-ed11-b874-9a7fd23bdbb9)/items?$filter=number eq 'PLATE*' or displayName eq '*PLATE*'

However, this is not a valid request, as the service returns this error:

{
    "error": {
        "code""BadRequest_MethodNotImplemented",
        "message""The 'OR' operator is not supported on distinct fields on an OData filter.  CorrelationId:  00c5784c-da54-4622-b452-a85648391af9."
    }
}

This is quite limiting, as we can't figure out a way to implement a powerful search engine, and in SaaS BC where we don't have a SQL database access.

We would like to avoid the approach of implementing event subscribers in the Item table, and be constantly updating data in a external database that we would use for our search engine, but that's the only path we have come up with so far.

I've noticed that list pages in BC have a search functionality that is able to search in all fields; wondering if there's a way to access this control through API

pastedimage1684314226042v1.png

Another approach could be using multiple API request and combining them, but this could have a poor performance, as on top of multiple request, we would need to filter out duplicates

Categories:
  • Suggested answer
    Haliax Profile Picture
    Haliax 2 on at
    Implementing an Items search engine with Business central API
    I came across the same problem, so this might help.
     
    You need to add a flowfilter field in the table, and publish it in the API page, so you can filter it like any other field.
    Then, add something like this in Api page's OnOpenPage trigger:
        trigger OnOpenPage()
        var
            filter_: Text;
        begin
            filter_ := Rec.GetFilter(searchFilter);
            if filter_ <> '' then begin
                Rec.FILTERGROUP := -1;
                Rec.SetFilter("Description", filter_);
                Rec.SetFilter("Search Description", filter_);
            end;
        end;
    Adding "FILTERGROUP := -1" enables cross-column search. Remember to get and save the filter value before setting FILTERGROUP, or it will be empty
     
    /items?$filter=searchFilter eq 'chair'
     
     
  • Suggested answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 17,957 Super User 2024 Season 2 on at
    RE: Implementing an Items search engine with Business central API

    Hope this helps

    [View:https://youtu.be/TSQiE42tDwU:320:240]

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Implementing an Items search engine with Business central API

    Honestly Manmeet I dont know What does your answer bring to the table..

    Just asking for ideas about the best way to implement a item search engine where BC is the datasource

    Obviously, one single field search is not enough for our case, so the standard Odata web service is of no help as I posted before.

  • Suggested answer
    Manish Kutar Profile Picture
    Manish Kutar 45 on at
    RE: Implementing an Items search engine with Business central API

    Hi,

    If you would have gone through the OData filter expressions supported on BC: Using Filter Expressions in OData URIs - Business Central | Microsoft Learn, it clearly mentions that OR filters cannot be used to apply filters on two different fields.

    pastedimage1684486367074v1.png

    In such case, you would have to change the approach of including only one field for searching with OR filter expressions.

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Implementing an Items search engine with Business central API

    OK! Last call here, in case anyone un the community has an idea.. we really need to implement a item search engine where BC is the datasource

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,252 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,089 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans