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...
Suggested Answer

Which trigger to implement this?

(0) ShareShare
ReportReport
Posted on by 358

I am trying to write code on Sales Order's subform;

Quantity Remaining = Quantity - Quantity Shipped - Reserved Quantity

Quantity Remaining is a new field I added by extending the Sales Line table and Sales Order Subform.

This value will be Quantity - Quantity Shipped - Reserved Quantity

However, Quantity Shipped only gets a value once the SO is shipped with the value in Qty to Ship.

I tried writing the followling code on ExtendedSalesLineTable

 trigger OnModify()
    var

    begin
        IF rec."Quantity Shipped" <> 0 then begin
            recSalesLine.SetFilter("No.", Rec."No.");
            recSalesLine.SetRange("Line No.", Rec."Line No.");
            recSalesLine.SetFilter("Document No.", Rec."Document No.");
            if recSalesLine.FindFirst() then begin
                "Quantity Remaining" := (Rec.Quantity - Rec."Quantity Shipped") - rec."Reserved Quantity";
                recSalesLine.Modify();
            end;
        end;
    end;

The problem is, I get Not Available in Rec.Quantity Shipped hence it always show me new field Remaining Quantity as 0.

I have the same question (0)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,300 Super User 2026 Season 1 on at

    Do a subscription to codeunit 90 on

    OnBeforePostUpdateOrderLineModifyTempLine(TempSalesLine,WhseShip,WhseReceive,SuppressCommit);

    Update the TempSalesLine, you will get the updated Quantity Remaining when you post.

  • mysamza Profile Picture
    358 on at

    Hello @Teddy Herryanto.

    I attempted your method and here is what I came up with;

    Codeunit 90 does not have OnBeforePostUpdateOrderLineModifyTempLine Event.

    I found it on CodeUnit 80 Sales-Post.

    However upon a little testing, where I just tried to execute  message('Did this execute);

    on OnBeforePostUpdateOrderLineModifyTempLine procedure and posted an SO. It did not show the message so I am assuming the OnBeforePostUpdateOrderLineModifyTempLine did not execute.

    However when I did the same for Sales-post Yes/No and chose for example onafterpost event message for this procedure did execute.

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostUpdateOrderLineModifyTempLine', '', true, true)]
        procedure MyProcedure(var TempSalesLine: Record "Sales Line"; WhseShip: Boolean; WhseReceive: Boolean; CommitIsSuppressed: Boolean)
    
        var
            recSalesLine: Record "Sales Line";
        begin
           
            TempSalesLine."Quantity Remaining" := recSalesLine."Quantity Shipped" - recSalesLine.Quantity;
         
        end;

  • Suggested answer
    Olister Rumao Profile Picture
    4,009 on at

    Hi Mesam,

    You can write a local procedure say UpdateRemainingQty where you write the logic.

    Then on validate trigger of all the 3 fields you call the local procedure.

    And instead of creating a variable and not using it anywhere

    Simply write 

    Rec.QuantityRemaining := Rec.Quantity - Rec.QuantityShipped - Rec.QuantityReserved;

    As you don't need to write Rec.Modify.

    If the page doesn't refresh immediately simply use CurrPage.Update.

  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,300 Super User 2026 Season 1 on at

    Sorry, it is codeunit 80.

    You can't do it like that because the variable recSalesLine is empty.

    Do it like this. You don't need new variable.

    TempSalesLine."Quantity Remaining" := TempSalesLine."Quantity Shipped" - TempSalesLine.Quantity;

  • Suggested answer
    mysamza Profile Picture
    358 on at

    I did the following and it worked!

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post (Yes/No)", 'OnAfterPost', '', true, true)]

       procedure MyProcedure(var SalesHeader: Record "Sales Header")

       var

           recSalesLine: Record "Sales Line";

           nocode: Code[20];

       begin

           nocode := SalesHeader."No.";

           recSalesLine.SetFilter("Document Type", 'Order');

           recSalesLine.SetFilter("Document No.", nocode);

           if recSalesLine.FindSet() then begin

               recSalesLine."Quantity Remaining" := recSalesLine.Quantity - recSalesLine."Quantity Shipped" - recSalesLine."Reserved Quantity";

               recSalesLine.Modify();

           end;

       end;

  • mysamza Profile Picture
    358 on at

    Hello, I tried your method and so did I continue making the event/subscribe method work as mentioned by @Teddy and it worked! Posted the code below if you want to have a look! Thanks though!

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 2,033 Super User 2026 Season 1

#2
Dhiren Nagar Profile Picture

Dhiren Nagar 1,105 Super User 2026 Season 1

#3
YUN ZHU Profile Picture

YUN ZHU 1,035 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans