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

Adding average field from Item Ledger Entry table breaks the Item Invoicing FactBox

(0) ShareShare
ReportReport
Posted on by 383
Hello!
 
I want to add a field to the /Item Invoicing FactBox/ in Business Central. This is the box that appears to the right when clicking on an item in the Items page.
 
 
I want this field to show the average amount that item is being sold for. I figured I could add a field to the Item table that gets the average of the /Sales Amount (Actual)/ from the /Item Ledger Entry/ table.
 
So I tried adding this to my Item table:
 
field(50103; /Average Sales Amount/; Decimal)
        {
            CalcFormula = average(/Item Ledger Entry/./Sales Amount (Actual)/ WHERE(/Item No./ = field(/No./)));
            Caption = 'Average Sales Amount';
            Editable = false;
            FieldClass = FlowField;
        }
 
Then I tried adding that field to the field to the /Item Invoicing FactBox/ page like this:
 
pageextension 50123 ItemInvFactBoxExt extends /Item Invoicing FactBox/
{
    layout
    {
        addlast(content)
        {
            field(/Average Sales Amount/; Rec./Average Sales Amount/)
            {
                ApplicationArea = All;
            }
        }
    }
}
 
But that gives me an error in the FactBox saying:
Error: An unexpected error occurred after a database command was cancelled..
 
 
I tried adding another couple of fields from the Item Ledger Entry table, such as /Qty. On Sales Order/ which is quite similar in a way, since it is a CalcFormula sum field from the same table. I am able to add that field, so it seems to me like there is a problem with the /average/ field in this particular case.
 
How can I get the field I want to show in the FactBox without breaking it?
I have the same question (0)
  • Suggested answer
    KasparsSemjonovs Profile Picture
    4,821 Super User 2026 Season 1 on at
    I see no exception in calculation if there are no Sales done... and it would result in division by zero which could be the cause of the error.
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    26,803 Super User 2026 Season 1 on at
    Hi @ChristianPIT,
    it seems to me that the problem is due to the "Sales Amount (Current)" field, since it is FlowField
    whatever please try this code in your page extension :
    pageextension 50123 ItemInvFactBoxExt extends "Item Invoicing FactBox"
    {
        layout
        {
            addlast(content)
            {
                field(AvgSalesAmount; AvgSalesAmount)
                {
                    ApplicationArea = All;
                    Caption = 'Average Sales Amount';
                }
            }
        }
    
        trigger OnAfterGetCurrRecord()
        var
            ItemLedgerEntry: Record "Item Ledger Entry";
            counter: Integer;
            sum: Decimal;
        begin
            ItemLedgerEntry.SetRange("Item No.", Rec."No.");
            if ItemLedgerEntry.FindSet() then
                repeat
                    ItemLedgerEntry.CalcFields("Sales Amount (Actual)");
                    sum := sum + ItemLedgerEntry."Sales Amount (Actual)";
                    counter := counter + 1;
                until ItemLedgerEntry.Next() = 0;
    
            if counter = 0 then
                AvgSalesAmount := 0
            else begin
                AvgSalesAmount := sum / counter;
            end;
        end;
    
        var
            AvgSalesAmount: Decimal;
    }
    Best regards,
    Mohamed Amine MAHMOUDI

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,012 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 986 Super User 2026 Season 1

#3
Teagen Boll Profile Picture

Teagen Boll 659 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans