Add the total of a column on a list page

On the “Purchase Invoices” page, I want to create a “Totals” section, where the “Amount” and “Amount including VAT” fields of all records are added. To achieve this, I am using the following code:

trigger OnAfterGetCurrRecord()
var
PurchaseHeader: Record “Purchase Header”;
begin
PurchaseHeader.CopyFilters(Rec);
PurchaseHeader.CalcSums(Amount, “Amount Including VAT”);
TotalAmount := PurchaseHeader.Amount;
TotalAmountIncludingVAT := PurchaseHeader.“Amount Including VAT”;
end;

But it doesn’t work, variables always appear in “0”. Use this same code in a page created by myself and there if it worked, do I have to change something as it is a default page or am I making a mistake? Some alternative?

It looks like you’re using the correct code to calculate the totals of the purchase invoices, but the issue may be with the “CopyFilters” function. This function is used to copy the filter values from the current record to another record, but in your case, it may not be necessary.

Try removing the “CopyFilters” function and use the “SetRange” function to filter the “Purchase Header” records based on the fields you want to use for calculating the totals. You can also use the “Sum” function to calculate the totals directly.

Here’s an example of how your code could be modified:

trigger OnAfterGetCurrRecord()
var
PurchaseHeader: Record “Purchase Header”;
begin
PurchaseHeader.SetRange(“Document Type”,"Document Type”);
PurchaseHeader.SetRange(“Vendor No.”,"Vendor No.”);
PurchaseHeader.CalcSums(Amount, “Amount Including VAT”);
TotalAmount := PurchaseHeader.Sum(Amount);
TotalAmountIncludingVAT := PurchaseHeader.Sum(“Amount Including VAT”);
end;

This code will calculate the totals for the “Amount” and “Amount Including VAT” fields of all “Purchase Header” records that match the filter criteria. If you have any further questions, please let us know.

Hello,
the most likely reason why is not working is because you are summing flows fields (those 2 fields are flowfiels) ad they are not calculated.

I strongly advise you not to do this. This will have a tremendous impact on the page performance and may make the age unusable.
Put it in a report or Put an action that can be pressed and then display those values in a message or so.
don’t make all hove calculations all the time on top of flow fields.

Thank you,

Nun Pinto
Lidd Consultants