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

Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

(0) ShareShare
ReportReport
Posted on by
codeunit 50103 ModifySaleLines
{
    trigger OnRun()
    begin
        SalesLines.Reset();
        SalesLines.SetRange("Document No.", 'S-ORD101032');
        decQty := 0.00;
        IF SalesLines.FIND('-'THEN
            repeat
                decQty += SalesLines.Quantity;
            until SalesLines.Next = 0;
        SalesLines."Sum of Quantity" := decQty;
        SalesLines.Modify(true);
        Message('%1', SalesLines."Sum of Quantity");

    end;

    var
        SalesLines: Record "Sales Line";
        decQty: Decimal;
}
TableExtension
          
tableextension 50103 SalesLinesTable extends "Sales Line"
{
    fields
    {
        // Add changes to table fields here
        field(50101; "Sum of Quantity"; Decimal)
        {

           
        }

    trigger OnModify()
    begin
        CodeUnit3.Run();
    end;

    var
        ILE: Record "Sales Line";
        decqty: Integer;
         CodeUnit3: Codeunit ModifySaleLines;
}
Can anybody tell me why I am not getting some of sale line items quantity in another field. Where I am wrong?
I have the same question (0)
  • Suggested answer
    keoma Profile Picture
    32,729 on at

    the modify trigger of a table is called if you change/add a value in that table.

    this is not called if you only display the table ... or  - in your case - the sales order with the sales lines.

    but ... if you really change a value in a sales line in your sales order, then the modify trigger is called, that calls the run fuction in the codeunit, which calls the modify trigger of the sales line, which means you created an endless loop!!

  • Community Member Profile Picture
    on at

    Modify trigger is working like when I am changing the value in Quantity field of sales Line items on sales order page. You are right I am getting different value in message box because of endless loop but why loop is running more than condition

     IF SalesLines.FIND('-') THEN

               repeat

                   decQty += SalesLines.Quantity;

               until SalesLines.Next = 0;

           SalesLines."Sum of Quantity" := decQty;

           SalesLines.Modify(true);

           Message('%1', SalesLines."Sum of Quantity");

       end;

    I am new in AL code , It would be very kind of you if you can help me to solve this problem. Thank You

  • Community Member Profile Picture
    on at

    When I am using below code unit on open page trigger -

    trigger OnOpenPage()

       begin

           CodeUnit1.Run();

       end;

    codeunit 50100 OpenSalesLines

    {

       trigger OnRun()

       begin

           CLEAR(SalesLines."Sum of Quantity");

           Clear(decQty);

           SalesLines.Reset();

           SalesLines.SetRange("Document No.", 'S-ORD101032');

           IF SalesLines.FindSet THEN

               SalesLines."Sum of Quantity" := 0.00;

           decQty := 0.00;

           repeat

               decQty += SalesLines.Quantity;

           until SalesLines.Next = 0;

           SalesLines."Sum of Quantity" := decQty;    // storing sum of sales line items in custom field Sum Of Quantity

           Message('%1 Open Page', SalesLines."Sum of Quantity");

           /* SalesLines.Modify(true); */

       end;

       var

           SalesLines: Record "Sales Line";

           SalesHeader: Record "Sales Header";

           decQty: Decimal;

    }

    In this case before opening sales order page I am getting sum of quantity in message box right but when page is opened then inside sum of quantity field I am getting wrong value. Why is this so?

  • Suggested answer
    keoma Profile Picture
    32,729 on at

    please describe what you are supposed to have in the end, describe the process, the function you want to have.

    normally it's not needed to have the qty sum for all lines in a field of each line. but if really needed, this is done by a flowfield.

    in table sales header there is a field for that sum, which is shown in a field in the sales order subform under the lines.

  • Community Member Profile Picture
    on at

    8371.Capture.PNGHi, Thank you reply.

    My requirement is that I have to get sum of quantity field of all sale lines items and then show it into custom field(Sum of Quantity)

  • Suggested answer
    keoma Profile Picture
    32,729 on at

    try following:

    pageextension 50101 SalesOrderSubformExt extends "Sales Order Subform"

    {

       layout

       {

           addlast(Control51) // the panel control, which contains the sum fields under the lines

           {

               field(TotalQty; TotalQty)

               {

                   Caption = 'Total Qty';

                   Editable = false;

               }

           }

       }

       trigger OnAfterGetCurrRecord()

       begin

           SalesLine.CopyFilters(Rec);

           SalesLine.CalcSums(Quantity);

           TotalQty := SalesLine.Quantity;

       end;

       var

           TotalQty: Decimal;

           SalesLine: record "Sales Line";

    }

  • Community Member Profile Picture
    on at

    Actually, I tried but the think is that why field is not visible on page for both Control51 and Control 45??

  • keoma Profile Picture
    32,729 on at

    did you publish the extension?

  • Community Member Profile Picture
    on at

    Yes, I forgot to add Application area=all;

    It's working. But when I modify the quantity or adding new line it's not updation same time until I click on next line. Why someone will click on next line?....

  • Community Member Profile Picture
    on at

    Yes, I forgot to add Application area=all;

    It's working. But when I modify the quantity or adding new line it's not updation same time until I click on next line. Why someone will click on next line?....

    Like Total Excl. Tax(CAD) and some other fields change on the same time as we enter price.

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,664

#2
YUN ZHU Profile Picture

YUN ZHU 960 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 773 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans