Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

Apply Filter Text to TableRelation?

Posted on by 10

Hi All,

This is the scenario. 

I am working on this extension where a user is able to pick a product category to filter a dropdown of Items.
Because this user is also able to select a parent category I wrote a CodeUnit that takes in a Item Category and returns a Text containing itself and all of the sub categories:

Example:

The table I am working with is my own table "Project Item"

User chose item category: 'Projection Surfaces'
The Codeunit returns: 'Projection Surfaces|Projection Fabrics|Projection Boards|Projection Screens'
This response it put in the field Rec."Category Filter" where Rec is a "Project Item" and "Category Filter" is a field of type Text[100.

I would like to apply this filter to the TableRelation of the ProjectItem."No." field.
I tried this:

field(4; "Item No."; Code[20])
{
    Caption = 'Item No.';
    DataClassification = CustomerContent;

    TableRelation = if ("Category Filter" = filter(<> '')) Item where("Item Category Code" = field("Category Filter"))
    else
    Item;

}
Now this is not working like I want it to.
When a user selects an item category which results in only that item category being returned such as "Projection Screens" then the filter is working and filters the "No." field correctly. However, when the user chose an item category that has child categories and I end up with a filter like  'Projection Surfaces|Projection Fabrics|Projection Boards|Projection Screens' I get errors about lengths (65) are exceeding the Code[20 length. I get this, but how can I correctly apply this text filter to a table relation?

N.B.
I did manage to sort this out using custom OnLookup logic but it seems counter intuitive writing this custom logic because it felt I was so close to the solution before.
Thanks for taking the time to help me out here!
T.
  • RE: Apply Filter Text to TableRelation?

    Hi, good question.

    I would have suggested to use Filter instead of Field, like this:

    TableRelation = if ("Category Filter" = filter(<> '')) Item where("Item Category Code" = filter("Category Filter"));
    But quick tests here seems that that doesn't work either. I can set a filter like this in the old dev client:
    IF (Category Filter=FILTER(<>'')) Item WHERE (Item Category Code=FILTER(CATEGORY FILTER))
    So I wonder if the standard application is using anything like this somewhere, then that should show how to do it. Otherwise you may have to code the OnLookup trigger like you say. Also I look forward to hearing other suggestiopns.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans