web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

AL Code: Sales Order Subform: Filter Item No. Dropdown depending on Sales Header Information

(7) ShareShare
ReportReport
Posted on by 23
Hi everyone!
I have the following requirement in the Sales Order: The items showed in the Dropdown from the No. and description field should be filtered by information that is in the Sales Header of this Sales Order.
 
If this would be a popup it would be an easy solution because I could write it in the Lookup trigger of the field... But we want it as dropdown. And this can only be filtered directly through the TableRelation property of that field. To "overwrite" the existing property, I created a new No. field (so I have full control over the TableRelation Cases). The problem I get into is:
 
TableRelation = <TableName>[.<FieldName>] [WHERE(<TableFilters>)] |
[IF(<Conditions>) <TableName>[.<FieldName>] [WHERE(<TableFilters>)]
 
In the Condition part, I can only filter my cases by fields that are already in this particular Sales Line. So no information of the Sales Header is reachable (or is it?):
 
(the following code is in a page extension to sales order subform).
//my new field
field(BufferNo; BufferNo)
            {
                ApplicationArea = Basic, Suite;
                Caption = 'Custom Nr.';
                //LookupPageID = "Option Lookup List";
                ShowMandatory = not IsCommentLine;
                TableRelation = if (Type = const(" ")) "Standard Text"
                else
                if (Type = const("G/L Account"), "System-Created Entry" = const(false)) "G/L Account" where("Direct Posting" = const(true), "Account Type" = const(Posting), Blocked = const(false))
                else
                if (Type = const("G/L Account"), "System-Created Entry" = const(true)) "G/L Account"
                else
                if (Type = const(Resource)) Resource
                else
                if (Type = const("Fixed Asset")) "Fixed Asset"
                else
                if (Type = const("Charge (Item)")) "Item Charge"
                else
                if (Type = const("Allocation Account")) "Allocation Account"
                else
                if (Type = const(Item), "Document Type" = filter(<> "Credit Memo" & <> "Return Order"), Debitorenart = const('AUD')) Item where(Blocked = const(false), "Sales Blocked" = const(false), "Item Category Code" = const('STANDARD'))
                else
                if (Type = const(Item), "Document Type" = filter(<> "Credit Memo" & <> "Return Order"), Debitorenart = const('AT')) Item where(Blocked = const(false), "Sales Blocked" = const(false), "Item Category Code" = const('AT'))
                else
                if (Type = const(Item), "Document Type" = filter("Credit Memo" | "Return Order")) Item where(Blocked = const(false));
if we look at this line:
if (Type = const(Item), "Document Type" = filter(<> "Credit Memo" & <> "Return Order"), Debitorenart = const('AUD')) Item where(Blocked = const(false), "Sales Blocked" = const(false), "Item Category Code" = const('STANDARD'))
 
I want to add a condition to a field called "Debitorenart" which is found in the header (field Code[10]). Now my thinking was: I can copy this field into the sales line from the sales header by an eventsub somewhere close to "OnInit" of the Sales Line, so I can grab the information in the TableRelation. But it doesn't work...
 
The only eventsub that works is OnBeforeInsertEvent or OnAfterInsertEvent. But that's already too late, because the dropdown is usually opened before the Line is inserted. I tried something like OnAfterInitType which is pretty close after normal Init, but it does not write any value into the field. Which confuses me, because somehow it is able to filter for Document type, which had to be given to the Line at one point.
 
//This one works... But too late 
[EventSubscriber(ObjectType::table, 37, OnAfterInsertEvent, '', false, false)]
    procedure UpdateLiefzeilen(var Rec: Record "Sales Line"; RunTrigger: Boolean)
    var
        SalesHeader: Record "Sales Header";
    begin
        SalesHeader.SetRange("Document Type", Rec."Document Type");
        SalesHeader.SetRange("No.", Rec."Document No.");
        if SalesHeader.FindFirst() then begin
            Rec.Validate("Debitorenart", SalesHeader.Debitorenart);
            Rec.Modify();
        end;
    end;
Does anyone have any suggestion on how to solve this? I only see the following ways:
- Somehow be able to get the information directly from the Sales Header in the Table Relation of the pageextension
- Get the information in the SalesLine directly after Init and have it accessible by TableRelation property
- Maybe work with some kind of BufferTable like the Type field does? 
 
I am really out of ideas here how to solve this and am grateful for any input.
I have the same question (0)
  • Verified answer
    Yi Yong Profile Picture
    2,563 Super User 2025 Season 2 on at
    Hello,
     
    You can use the OnNewRecord trigger event on the page extension.
    This is called after the new record is initialized but before inserted to the table, so it will not be "too late".
     
    pageextension 50010 Page10 extends "Sales Order Subform"
    {
        layout
        {
            addafter("No.")
            {
                field(Debitorenart; Rec.Debitorenart)
                {
                    ApplicationArea = All;
                }
                field(BufferNo; Rec."Buffer No.")
                {
                    ApplicationArea = All;
                }
            }
        }
    
        trigger OnNewRecord(BelowxRec: Boolean)
        var
            SH: Record "Sales Header";
        begin
            SH := Rec.GetSalesHeader();
            Rec.Debitorenart := SH."External Document No.";
        end;
    }
  • AG-21040804-0 Profile Picture
    23 on at
    Oh wow, okay... Thank you very much for bumping me into this solution. I tried this trigger 2 days ago, but before I replicated the approach of the Type field and never reversed the tablerelation into the Custom TableField from the Custom Variable Field in the PageExt... No wonder all my tries weren't working since then.
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,441 Super User 2025 Season 2 on at

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,468

#2
YUN ZHU Profile Picture

YUN ZHU 923 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans