Skip to main content

Notifications

Announcements

No record found.

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.

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,274 Super User 2024 Season 1 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    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

  • Suggested answer
    Amit_Sharma Profile Picture
    Amit_Sharma 2,545 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    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
    Prtham Profile Picture
    Prtham 171 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    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;

  • CRSW Profile Picture
    CRSW 593 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    Hi

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

  • Suggested answer
    Chameera Kavinda Profile Picture
    Chameera Kavinda 510 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    Try CurrPage.Update at the end of the code

  • Suggested answer
    Akshay_Panchal Profile Picture
    Akshay_Panchal 402 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    Hii,

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

    Thanks.

    Verify, If this answer helpful to you.

  • Samantha73 Profile Picture
    Samantha73 2,490 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    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
    MahGah Profile Picture
    MahGah 15,433 on at
    RE: Update Sales Header total values when inserting a sales line programmatically

    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?

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans