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

Update Sales Header total values when inserting a sales line programmatically

(0) ShareShare
ReportReport
Posted on by 120

Hi All,

I'm doing a customization for Sales Order Card. The requirement is to add a new item line when releasing Sales Order. Adding a new line works fine. following is a code sample used for that.

pageextension 50100 SalesOrderExt extends "Sales Order"
{
    layout
    {

    }
    actions
    {
        modify(Release)
        {
            trigger OnBeforeAction()
            var
                SalesHeader: Record "Sales Header";
                AddPotLevyItem: Codeunit "Add Pot Levy Item";
            begin
                SalesHeader.Copy(Rec);
                AddPotLevyItem.AddItem(SalesHeader);
            end;
        }
    }
}

please note that, Amount of the new item is calculated from existing sales lines values.

    procedure AddItem(SalesHeader: Record "Sales Header")
    var
        SalesLine: Record "Sales Line";
        PotLevyAmount: Decimal;
        NewLineNo: Integer;
        NewSalesLine: Record "Sales Line";
        ItemType: Enum "Sales Line Type";
        ItemNo: Code[20];
    begin
        ItemNo := '1908-S';
        SalesLine.SetRange("Document Type", SalesHeader."Document Type");
        SalesLine.SetRange("Document No.", SalesHeader."No.");
        if SalesLine.FindSet() then begin
            repeat begin
                PotLevyAmount := SalesLine.Amount * (5 / 100);
            end until SalesLine.Next() = 0;

            SalesLine.FindLast();
            NewLineNo := SalesLine."Line No."   10000;

            NewSalesLine.Init();
            NewSalesLine."Document Type" := SalesHeader."Document Type";
            NewSalesLine."Document No." := SalesHeader."No.";
            NewSalesLine."Line No." := NewLineNo;
            NewSalesLine.Validate(Type, ItemType::Item);
            NewSalesLine.Validate("No.", ItemNo);
            if NewSalesLine.Insert(true) then begin
                NewSalesLine.Quantity := 1;
                NewSalesLine.Validate(Quantity);
                NewSalesLine.Amount := PotLevyAmount;
                NewSalesLine.Validate(Amount);
                NewSalesLine.Modify(true);
            end;
        end;
    end;

The issue is that, Sales line total values are not updated.

I highly appreciate your advice.

Thanks,

Nalin.

I have the same question (0)
  • Suggested answer
    MahGah Profile Picture
    15,583 on at

    Hi

    I am sure some developer will answer your question.

    But I assume since Sales Header total values are "sum flowfield over lines" type they may require some kind of action (such as release) to update them.

    Can you in your code first add the line then "release" the SO?

  • Samantha73 Profile Picture
    3,297 on at

    I'm having the same issue has anyone found a solution to when header totals are not getting updated when inserting lines via code?

  • Suggested answer
    Akshay_Panchal Profile Picture
    402 on at

    Hii,

    Try to call updateAmount function of sales line after inserting of new sales line.

    Thanks.

    Verify, If this answer helpful to you.

  • Suggested answer
    Chameera Kavinda Profile Picture
    510 on at

    Try CurrPage.Update at the end of the code

  • CRSW Profile Picture
    627 on at

    Hi

    Any solutions on that? Tried UpdateAmount AND currPage.Update without success

  • Suggested answer
    Prtham Profile Picture
    171 on at

    I think you are validating incorrectly.

    if NewSalesLine.Insert(true) then begin

      NewSalesLine.Validate(Quantity, 1);

      NewSalesLine.Validate(Amount, PotLevyAmount);

      NewSalesLine.Modify(true);

    end;

    Also, at the end of the code, don't forget to get the sales header 

    IF Rec.Get(Rec.DocumentType, Rec.Document No.) THEN;

  • Suggested answer
    Amit Profile Picture
    2,561 on at

    Hi,

    Use this property in sub-form page or you also achieve this requirement by using Flowfields feature. But below property is mandatory.

    UpdatePropagation = Both;

    Regards

    Amit Sharma

    www.erpconsultors.com

    Press Yes if Info is useful

  • Suggested answer
    DAnny3211 Profile Picture
    11,407 Super User 2026 Season 1 on at

    CIao
    try inserting a currpage.Update at the end of your code

    although honestly I would link more to the codeunit 414 'Release Sales Document' event:

    pastedimage1669566539513v1.png

    keep in mind that the totals on the order card should be calculated functions

    DAniele

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

#2
YUN ZHU Profile Picture

YUN ZHU 1,122 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 959 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans