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...
Suggested Answer

Sales Line Fact Box Card Part Total Cost field

(0) ShareShare
ReportReport
Posted on by 77

I need to calculate the total cost for the sales qoute record, there exists an existing field for total amount in sales qoute subform page, but there is no existing field for total cost. Is there a field that I an use to foreach all sales line value and manually calculate the cost?

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,698 Moderator on at

    Hi chigivigi ,

    You can use the Unit price field on sales Quote line.

    pastedimage1657292138948v1.png

    Thanks.

  • chigivigi Profile Picture
    77 on at

    I tried using Unit Price but I got 0 for the field, I tried other field like Unit Cost but they also returned 0.

    This is my code snippet:

    field("Total Cost"; TotalSalesLine."Unit Price")

    {

      ApplicationArea = Basic, Suite;

      Caption = 'Total Cost';

    }

  • Suggested answer
    Alfredo_Iorio Profile Picture
    1,262 on at

    Hi.

    The field that holds the Unit Cost of the item in the sales line is called Unit Cost (LCY).

    Screenshot-2022_2D00_07_2D00_12-152152.png

    You need to get that field, multiply it by the quantity for each line and you will get the total costs.

    I developed something similar for a client to show total costs, gross profit and gross margin on sales quotes and orders using similar logic.

  • Suggested answer
    Nitin Verma Profile Picture
    21,698 Moderator on at

    Please share the shole logic how you are extending 

  • Suggested answer
    Alfredo_Iorio Profile Picture
    1,262 on at

    I am afraid I cannot share the code.

    The logic is based on the field Unit Cost (LCY).

    First, you get the field then the unit of measure and the Qty per Unit of Measure. That's because the field Unit Cost (LCY) is always based on the Base unit of measure.

    Then you calculate the total cost for each line to get the total cost of the sales document.

    The gross profit is the difference between the Total Amount of the sales document lines and the total cost just calculated.

  • Suggested answer
    YUN ZHU Profile Picture
    95,329 Super User 2025 Season 2 on at

    Hi, Here is a simple example, you can modify it slightly.

    https://yzhums.com/25033/

    pastedimage1657674690060v1.png

    Hope this helps.

    Thanks.

    ZHU

  • chigivigi Profile Picture
    77 on at

    I am looking at your example and tried to adapt it to my factbox. I could not add modify keyword inside the layout.

    The first error is this:

    Syntax error, '}' expected

    The second error is this:

    Expected one of the application object keywords (table, tableextension, page, pageextension, pagecustomization, profile, codeunit, report, reportextension, xmlport, query, controladdin, dotnet, enum, enumextension, interface, permissionset, permissionsetextension, entitlement)

    If i try to create an extension for my factbox, there is no error on modify keyword but I get a dffirent error that looks like this:

    The extension object 'MyPageExtensionFactbox' cannot be declared. Another extension for target 'Sales Line Custom Factbox 2' or the target itself is already declared in this module.

    There is no second extension for my 'Sales Line Custom Factbox 2'.

    Do I declare the extension in another module, do I need to create a new Al Project and write the factbox extension there?

    My second question is regarding declaring new fields inside the factbox extension. I need to create four custom fields. Two fields are calculated by using the fields inside their Sales Line. The two other fields are calculated by adding those custom fileds result form each sales line.

    this is my code snippet:

    pageextension 56004 PageExtension56004 extends "Sales Line Custom Factbox 2"
    {
        layout
        {
            addafter(SalesUnitCalculation)
            {
                group(SalesLineCalculation)
                {
                    Caption = 'Sales Line Calculation';
    
                    field("Line Amount"; "Line Amount")
                    {
                        ApplicationArea = Basic, Suite;
                        Caption = 'Line Amount';
                    }
    
                    field(LineCost; LineCost)
                    {
                        Caption = 'Line Cost';
                        ApplicationArea = All;
                        Editable = false;
                        Visible = false;
    
                    }
                    field(LineProfit; LineProfit)
                    {
                        Caption = 'Line Profit';
                        ApplicationArea = All;
                        Editable = false;
                        Visible = false;
                    }
                }
    
                group(SalesTotalCalculation)
                {
                    Caption = 'Sales Total Calculation';
                    field("Total Amount"; TotalAmount)
                    {
                        ApplicationArea = Basic, Suite;
                        Caption = 'Total Amount Incl. VAT';
                    }
    
                    field(TotalCost; TotalCost)
                    {
                        Caption = 'Total Cost';
                        ApplicationArea = All;
                        Editable = false;
                    }
                    field(TotalProfit; TotalProfit)
                    {
                        Caption = 'Total Profit';
                        ApplicationArea = All;
                        Editable = false;
                    }
                }
            }
            modify("Unit Cost")
            {
                trigger OnAfterValidate()
                begin
                    UpdateLineCostAndLineProfit();
                    UpdateTotalCostAndTotalProfit();
                end;
            }
            modify(Quantity)
            {
                trigger OnAfterValidate()
                begin
                    UpdateLineCostAndLineProfit();
                    UpdateTotalCostAndTotalProfit();
                end;
            }
        }
    
        trigger OnAfterGetRecord()
        begin
    
        end;
    
        trigger OnAfterGetCurrRecord()
        begin
            UpdateLineCostAndLineProfit();
            UpdateTotalCostAndTotalProfit();
        end;
    
        trigger OnInsertRecord(BelowxRec: Boolean): Boolean
        begin
            UpdateLineCostAndLineProfit();
            UpdateTotalCostAndTotalProfit();
        end;
    
        trigger OnDeleteRecord(): Boolean
        begin
            UpdateLineCostAndLineProfit();
            UpdateTotalCostAndTotalProfit();
        end;
    
        var
            LineCost: Decimal;
            LineProfit: Decimal;
            TotalCost: Decimal;
            TotalProfit: Decimal;
    
        /// 
        /// UpdateLineCostAndLineProfit.
        /// 
        procedure UpdateLineCostAndLineProfit()
        var
            SalesLine: Record "Sales Line";
        begin
            LineCost := 0;
            LineProfit := 0;
            SalesLine.Reset();
            SalesLine.CopyFilters(Rec);
            LineCost := Quantity * "Unit Cost";
            LineProfit := "Line Amount" - LineCost;
        end;
    
        /// 
        /// UpdateTotalCostAndTotalProfit.
        /// 
        procedure UpdateTotalCostAndTotalProfit()
        var
            SalesLine: Record "Sales Line";
        begin
            TotalCost := 0;
            TotalProfit := 0;
            SalesLine.Reset();
            SalesLine.CopyFilters(Rec);
            SalesLine.CalcSums(LineCost);
            SalesLine.CalcSums(LineProfit);
            TotalCost := SalesLine.LineCost;
            TotalProfit := SalesLine."Line Amount" - TotalCost;
        end;
    }

    I get the folowing errors:

    Argument 1: must be a member

    'Record "Sales Line"' does not contain a definition for 'LineCost'

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 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans