Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Which trigger to implement this?

Posted on by 350

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.

Categories:
  • mysamza Profile Picture
    mysamza 350 on at
    RE: Which trigger to implement this?

    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!

  • Suggested answer
    mysamza Profile Picture
    mysamza 350 on at
    RE: Which trigger to implement this?

    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;

  • Suggested answer
    TeddyH Profile Picture
    TeddyH 12,868 Super User 2024 Season 1 on at
    RE: Which trigger to implement this?

    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
    Olister Rumao Profile Picture
    Olister Rumao 3,957 on at
    RE: Which trigger to implement this?

    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.

  • mysamza Profile Picture
    mysamza 350 on at
    RE: Which trigger to implement this?

    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
    TeddyH Profile Picture
    TeddyH 12,868 Super User 2024 Season 1 on at
    RE: Which trigger to implement this?

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans