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?
  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    Hi Franz,

    I have one question on sales order page , as we have sale lines in that I have added net weight field and extended weight custom field. My question is that how I can get quantity * Net weight value into extended weight field? Can we do multiply any operation for sales lines????

    Thank You

  • Suggested answer
    keoma Profile Picture
    32,725 on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    you're welcome.

    interesting. the code worked for me without currpage.update.

    please set one of my answers to verified.

  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    it's working.

    modify(Quantity)

          {

              trigger OnAfterValidate()

              begin

                  UpdateTotalQty();

    currPage.Update();                    

              end;

          }

    It was not updationg the page on modify event. When I added CurrPage.Update(); Then It started working.

    Thank you so much for your help.

  • Suggested answer
    keoma Profile Picture
    32,725 on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    use trigger OnAfterValidate instead of OnBeforeValidate.

  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    Hi Franz Kalchmair,

    I really appreciate your help. I copy and paste your code and publish it. But still I am not getting the require result. You can see below attachment.7002.Capture.PNG

    When I modify quantity 3 to 6 and then I clicked in the qty to assemble to order field in the same row. It did not updated. However, When I click on below or above any field then it's works.

  • Verified answer
    keoma Profile Picture
    32,725 on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    that works:

    pageextension 50104 SalesOrderSubformExt extends "Sales Order Subform"

    {

       layout

       {

           addlast(Control51)

           {

               field(TotalQty; TotalQty)

               {

                   Caption = 'Total Qty';

                   Editable = false;

                   ApplicationArea = all;

               }

           }

           modify(Quantity)

           {

               trigger OnAfterValidate()

               begin

                   UpdateTotalQty();

               end;

           }

           modify("Unit Price")

           {

               trigger OnAfterValidate()

               begin

                   UpdateTotalQty();

               end;

           }

       }

       procedure UpdateTotalQty()

       var

           salesLine: record "Sales Line";

       begin

           salesLine.CopyFilters(Rec);

           salesLine.CalcSums(Quantity);

           TotalQty := salesLine.Quantity;

       end;

       trigger OnAfterGetCurrRecord()

       begin

           UpdateTotalQty();

       end;

       var

           TotalQty: Decimal;

    }

  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    I have tried below procedure as you said but not working.

    pageextension 50102 SalesOrderLinesPage extends "Sales Order Subform"

    {

       layout

       {

           modify(Quantity)

           {

               trigger OnBeforeValidate()

               begin

                   UpdateTotalQty();

               end;

           }

           addlast(Control45)

           {

               group("")

               {

                   field("Sum of Quantity"; TotalQty)

                   {

                       ApplicationArea = all;

                       Caption = 'Total Quantityy';

                       Editable = false;

                   }

                   field("Total # Medical Skids"; "Total # Medical Skids")

                   {

                       ApplicationArea = all;

                       Caption = 'Total # Medical Skids';

                   }

               }

           }

           addlast(Control28)

           {

               field("Sum of Weight"; TotalWght)

               {

                   ApplicationArea = all;

                   Caption = 'Total Weight(LBS)';

               }

               field("Total # Supply Skids"; "Total # Supply Skids")

               {

                   ApplicationArea = all;

                   Caption = 'Total # Supply Skids';

               }

           }

       }

       procedure UpdateTotalQty();

       begin

           SalesLine.CopyFilters(rec);

           SalesLine.CalcSums(Quantity);

           TotalQty := SalesLine.Quantity;

           SalesLine.CalcSums("Gross Weight");

           TotalWght := SalesLine."Gross Weight";

       end;

       trigger OnAfterGetCurrRecord()

       begin

           SalesLine.CopyFilters(rec);

           SalesLine.CalcSums(Quantity);

           TotalQty := SalesLine.Quantity;

           SalesLine.CalcSums("Gross Weight");

           TotalWght := SalesLine."Gross Weight";

       end;

       var

           TotalQty: Decimal;

           SalesLine: record "Sales Line";

           TotalWght: Decimal;

    }

  • Suggested answer
    keoma Profile Picture
    32,725 on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    like in the standard code it's needed that the new field is also updated whenever the main fields unit price, quantity, etc are changed, the update code for totalqty is run. then totalqty is also updated when you leave the field within the line.

    so instead of

    trigger OnAfterGetCurrRecord()

      begin

          SalesLine.CopyFilters(Rec);

          SalesLine.CalcSums(Quantity);

          TotalQty := SalesLine.Quantity;

      end;

      var

          TotalQty: Decimal;

          SalesLine: record "Sales Line";

    create a procedure UpdateTotalQty with the above code.

    run that update code in the onvalidate trigger of the sales line fields, which you want them to update the new field.

    e.g.

    ...

    layout

    {

    ...

           modify("Unit Price")

           {

               trigger OnAfterValidate()

               begin

                   UpdateTotalQty();

               end;

           }

    }

  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    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.

  • Community Member Profile Picture
    on at
    RE: Get sum of sales Lines Quantity in separate field(SUM of Quantity) using Code unit

    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?....

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,157 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,930 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans