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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Sales Order Subform Conditional ShowMandatory

(7) ShareShare
ReportReport
Posted on by 765
Trying to add a procedure that will determine if the Location Code field on the Sales Order Subform should show as Mandatory based on the Sales Line Item and Item.Type.  Works fine for the Item being entered, but then also updates the setting for ALL items in the Subform based on the current record.  Below is the procedure that I am calling from the OnAfterValidate trigger of the No. field in the Subform.  I only want it to change the boolean value on the line being modified.
 
What am I missing? Is there a way to do that? Visual below.  Before the last line item was entered, the 2nd-4th lines did not show the asterisk, the 5th line should have the fields mandatory, when the Item was entered, all the lines changed.
 
modify("No.")
        {
            trigger OnAfterValidate()
            begin
                ShowMandatoryResults()
            end;
        }
 
        modify("Location Code")
        {
            ShowMandatory = ShowMandatoryValue;
        }
 
    procedure ShowMandatoryResults()
    begin
        SalesLine.SetRange("Document Type", SalesDocumentType::Order);
        SalesLine.SetRange("Document No.", Rec."Document No.");
        SalesLine.SetRange("Line No.", Rec."Line No.");
        if SalesLine.FindSet() then begin
            If SalesLine.Type <> SalesLineType::Item then
                exit
            else begin
                Item.SetRange("No.", SalesLine."No.");
                Item.SetRange(Type, ItemType::Inventory);
                if Item.FindSet() then
                    ShowMandatoryValue := true
                else
                    ShowMandatoryValue := false;
            end;
        end;
    end;
 
var
        SalesLine: Record "Sales Line";
        Item: Record Item;
        SalesLineType: Enum "Sales Line Type";
        SalesDocumentType: Enum "Sales Document Type";
        ItemType: Enum "Item Type";
        ShowMandatoryValue: Boolean;
I have the same question (0)
  • Suggested answer
    RockwithNav Profile Picture
    8,941 Super User 2026 Season 1 on at
    The only thing you need to make sure is - Is this getting triggered at the right time?
    Did you try with page triggers - OnAfterGetCurrRecord?
  • Suggested answer
    YUN ZHU Profile Picture
    99,055 Super User 2026 Season 1 on at
    Hi, Your code only works in the OnAfterValidate trigger of the No. field. This trigger will not be triggered when other fields are modified.
    The simplest way is to directly try the OnModifyRecord (Page) trigger. I think this can solve the problem.
     
    Thanks.
    ZHU
  • ME-31032107-0 Profile Picture
    765 on at
     
    I only want the trigger to execute when the No. field is modified, not when any other field on the page is modified. What is happening is that the newly entered line item is correct, but the value from the procedure ShowMandatoryResults() get applied to all the existing line items, and I don't want it to do that (they should remain as they were).
     
    Placing my procedure in the OnModify Trigger causes an infinity loop and insufficient memory issue, causing the page to close.
  • ME-31032107-0 Profile Picture
    765 on at
     
    I have tried it with the OnAfterGetCurrRecord, it does not change the behavior.
  • Verified answer
    Aman Kakkar Profile Picture
    2,977 Super User 2026 Season 1 on at
    Hi,
     

    You can handle this by using the OnAfterGetRecord trigger at the page level.

    Also, I’d recommend using GET instead of SetRange for filtering the Sales Line, since your filters are targeting the primary key fields of the Sales Line table.

    Please try the code below:

     
    
    pageextension 50040 "Sales Line Ext" extends "Sales Order Subform"
    {
        layout
        {
            modify("Location Code")
            {
                ShowMandatory = ShowMandatoryValue;
            }
        }
     
        trigger OnAfterGetRecord()
        begin
            if Rec."No." <> xRec."No." then begin
                if SalesLine.Get(SalesDocumentType::Order, Rec."Document No.", Rec."Line No.") then begin
                    if SalesLine.Type = SalesLineType::Item then begin
                        if Item.Get(SalesLine."No.") and (Item.Type = Item.Type::Inventory) then
                            ShowMandatoryValue := true
                        else
                            ShowMandatoryValue := false;
                    end else
                        ShowMandatoryValue := false;
                end;
            end;
        end;
     
        var
            SalesLine: Record "Sales Line";
            Item: Record Item;
            SalesLineType: Enum "Sales Line Type";
            SalesDocumentType: Enum "Sales Document Type";
            ItemType: Enum "Item Type";
            ShowMandatoryValue: Boolean;
     
    }
     
     
     
    Do mark as verified if this helps.
    Aman K
  • Suggested answer
    OussamaSabbouh Profile Picture
    12,766 Super User 2026 Season 1 on at
    Hello,
     
    Your issue happens because ShowMandatoryValue is a single variable shared by all lines.

    Fix: make it per-record by using [InDataSet] and update it in OnAfterGetRecord. Example:
     
    [InDataSet] ShowLocationMandatory: Boolean;
    trigger OnAfterGetRecord()
    begin
        ShowLocationMandatory := (Rec.Type = Rec.Type::Item) and (Rec."No." <> '');
    end;

    Then set:
    modify("Location Code")
    {
        ShowMandatory = ShowLocationMandatory;
    }

    Now, only the edited line updates, others stay unchanged.
     
    Regards,
    Oussama Sabbouh

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,926 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,158 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 533 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans