Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

Custom Factbox using the Sales Line Source Table problem with Total field

(0) ShareShare
ReportReport
Posted on by 77

I created a custom factbox for Sales Quote Details View in an AL Project. I would like to copy some lines from the Sales Line Subform and I ran into a problem when trying to copy Total Incl. Tax field. I tried to use the TotalSalesLine property found in the Sales Quote Subform and its connected procedures but its seems something is missing because the value displayed is always zero.

Can someone tell me what is missing in my code to make the field Total Incl. Tax work ?

This is my code:

page 56001 "Sales Line Custom Factbox"
{
    Caption = 'Sales Calculation';
    PageType = CardPart;
    SourceTable = "Sales Line";

    layout
    {
        area(content)
        {
group(SalesTotalCalculation)
            {
                Caption = 'Sales Total Calculation';
                field("Total Amount Incl. VAT"; TotalSalesLine."Amount Including VAT")
                {
                    ApplicationArea = Basic, Suite;
                    Caption = 'Total Amount Incl. VAT';
                    AutoFormatExpression = Currency.Code;
                    AutoFormatType = 1;
                    CaptionClass = DocumentTotals.GetTotalInclVATCaption(Currency.Code);
                }
            }
        }
    }

    actions
    {
    }

    trigger OnAfterGetCurrRecord()
    begin
        Rec.ClearSalesHeader();
    end;

    trigger OnAfterGetRecord()
    begin
        Rec.CalcFields("Reserved Quantity", "Attached Doc Count");
        SalesInfoPaneMgt.ResetItemNo();
        CalculateTotals();
    end;

    trigger OnOpenPage()
    begin
        SetOpenPage();
    end;

    var
        Currency: Record Currency;
        DocumentTotals: Codeunit "Document Totals";
        SuppressTotals: Boolean;
        TotalSalesHeader: Record "Sales Header";


    protected var
        SalesInfoPaneMgt: Codeunit "Sales Info-Pane Management";
        ItemAvailFormsMgt: Codeunit "Item Availability Forms Mgt";
        TotalSalesLine: Record "Sales Line";
        VATAmount: Decimal;
        InvoiceDiscountAmount: Decimal;
        InvoiceDiscountPct: Decimal;

    local procedure SetOpenPage()
    begin
        SuppressTotals := CurrentClientType() = ClientType::ODataV4;
    end;

    local procedure ShowNo(): Code[20]
    begin
        if Rec.Type <> Rec.Type::Item then
            exit('');
        exit(Rec."No.");
    end;

    local procedure GetTotalSalesHeader()
    begin
        DocumentTotals.GetTotalSalesHeaderAndCurrency(Rec, TotalSalesHeader, Currency);
    end;

    local procedure CalculateSalesLineCost() ReturnValue: Integer
    begin
        if Rec."Line Discount %" <> 0 then
            ReturnValue := "Unit Cost" * Quantity * ("Line Discount %" / 100)
        else
            ReturnValue := "Unit Cost" * Quantity
    end;

    procedure ClearTotalSalesHeader();
    begin
        Clear(TotalSalesHeader);
    end;

    procedure CalculateTotals()
    begin
        OnBeforeCalculateTotals(TotalSalesLine, SuppressTotals);

        if SuppressTotals then
            exit;

        DocumentTotals.SalesCheckIfDocumentChanged(Rec, xRec);
        DocumentTotals.CalculateSalesSubPageTotals(TotalSalesHeader, TotalSalesLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct);
        DocumentTotals.RefreshSalesLine(Rec);
    end;

    [IntegrationEvent(false, false)]
    local procedure OnBeforeCalculateTotals(var TotalSalesLine: Record "Sales Line"; SuppressTotals: Boolean)
    begin
    end;
}

pageextension 56002 PageExtension56002 extends "Sales Quote"
{
    layout
    {
        addbefore(Control1906127307)
        {
            part(CustomControl5653357897; "Sales Line Custom Factbox")
            {
                ApplicationArea = Suite;
                Provider = SalesLines;
                SubPageLink = "Document Type" = FIELD("Document Type"),
                              "Document No." = FIELD("Document No."),
                              "Line No." = FIELD("Line No.");
            }
        }
    }
}
  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Custom Factbox using the Sales Line Source Table problem with Total field

    Great.

  • Suggested answer
    chigivigi Profile Picture
    chigivigi 77 on at
    RE: Custom Factbox using the Sales Line Source Table problem with Total field

    You were right the problem was in the CalculateSalesLineCost procedure.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Custom Factbox using the Sales Line Source Table problem with Total field

    Hi,

    Please correct the below code in Yellow.

    local procedure CalculateSalesLineCost() ReturnValue: Decimal

       begin

           if Rec."Line Discount %" <> 0 then

               ReturnValue := "Unit Cost" * Quantity * ("Line Discount %" / 100)

           else

               ReturnValue := "Unit Cost" * Quantity

       end;

  • chigivigi Profile Picture
    chigivigi 77 on at
    RE: Custom Factbox using the Sales Line Source Table problem with Total field

    Thank you for the reply.

    Your solution works when Line Discount is not defined but when I set it the factbox crashes and an error message is displayed:

    Error: Overflow under conversion of Microsoft.Dynamics.Nav.Runtime.Decimal18 value 0.12 to System.Int32..

    pastedimage1657268579786v1.png

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Custom Factbox using the Sales Line Source Table problem with Total field

    Hi chigivigi ,

    Can you please try below code?

    pastedimage1657201408767v1.png

    page 56001 "Sales Line Custom Factbox"
    {
        Caption = 'Sales Calculation';
        PageType = CardPart;
        SourceTable = "Sales Line";

        layout
        {
            area(content)
            {
                group(SalesTotalCalculation)
                {
                    Caption = 'Sales Total Calculation';
                    field("Total Amount Incl. VAT"; TotalSalesLine."Amount Including VAT")
                    {
                        ApplicationArea = Basic, Suite;
                        Caption = 'Total Amount Incl. VAT';
                        AutoFormatExpression = Currency.Code;
                        AutoFormatType = 1;
                        CaptionClass = DocumentTotals.GetTotalInclVATCaption(Currency.Code);
                    }
                }
            }
        }

        // trigger OnAfterGetCurrRecord()
        // begin
        //     Rec.ClearSalesHeader();
        // end;

        trigger OnAfterGetRecord()
        begin
            Rec.CalcFields("Reserved Quantity", "Attached Doc Count");
            SalesInfoPaneMgt.ResetItemNo();
            GetTotalSalesHeader;
            CalculateTotals();
        end;

        trigger OnOpenPage()
        begin
            SetOpenPage();
        end;

        var
            Currency: Record Currency;
            DocumentTotals: Codeunit "Document Totals";
            SuppressTotals: Boolean;
            TotalSalesHeader: Record "Sales Header";


        protected var
            SalesInfoPaneMgt: Codeunit "Sales Info-Pane Management";
            ItemAvailFormsMgt: Codeunit "Item Availability Forms Mgt";
            TotalSalesLine: Record "Sales Line";
            VATAmount: Decimal;
            InvoiceDiscountAmount: Decimal;
            InvoiceDiscountPct: Decimal;

        local procedure SetOpenPage()
        begin
            SuppressTotals := CurrentClientType() = ClientType::ODataV4;
        end;

        local procedure ShowNo(): Code[20]
        begin
            if Rec.Type <> Rec.Type::Item then
                exit('');
            exit(Rec."No.");
        end;

        local procedure GetTotalSalesHeader()
        begin
            DocumentTotals.GetTotalSalesHeaderAndCurrency(Rec, TotalSalesHeader, Currency);
        end;

        local procedure CalculateSalesLineCost() ReturnValue: Integer
        begin
            if Rec."Line Discount %" <> 0 then
                ReturnValue := "Unit Cost" * Quantity * ("Line Discount %" / 100)
            else
                ReturnValue := "Unit Cost" * Quantity
        end;

        // procedure ClearTotalSalesHeader();
        // begin
        //     Clear(TotalSalesHeader);
        // end;

        procedure CalculateTotals()
        begin
            OnBeforeCalculateTotals(TotalSalesLine, SuppressTotals);

            if SuppressTotals then
                exit;

            DocumentTotals.SalesCheckIfDocumentChanged(Rec, xRec);
            DocumentTotals.CalculateSalesSubPageTotals(TotalSalesHeader, TotalSalesLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct);
            DocumentTotals.RefreshSalesLine(Rec);
        end;

        [IntegrationEvent(false, false)]
        local procedure OnBeforeCalculateTotals(var TotalSalesLine: Record "Sales Line"; SuppressTotals: Boolean)
        begin
        end;
    }

    pageextension 56002 PageExtension56002 extends "Sales Quote"
    {
        layout
        {
            addbefore(Control1906127307)
            {
                part(CustomControl5653357897; "Sales Line Custom Factbox")
                {
                    ApplicationArea = Suite;
                    Provider = SalesLines;
                    SubPageLink = "Document Type" = FIELD("Document Type"), "Document No." = FIELD("Document No."), "Line No." = FIELD("Line No.");
                }
            }
        }
    }

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

News and Announcements

Announcing Category Subscriptions!

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans