In the Sales Header record, there are FlowFields defined for Amount and "AmountIncludingVAT". These FlowFields are all based on Sums of entries in the SalesLine table. The CalcFormula for Amount is Sum("SalesLine".AmountWHERE(DocumentType=FIELD(DocumentType),DocumentNo.=FIELD(No.))).
CALCSUMS can be used on any integer, big integer, or decimal field with any filter on any table, but for larger datasets, creating a key with a SumIndexField is recommended.
To calculate a TotalOrderAmount value while referencing the Sales Header table, the code can be as simple as this:
"Sales Header".CALCFIELDS (Amount);
To use TotalOrderAmount := "Sales Header".Amount; to calculate the same value from code directly referencing the Sales Line table, the required code is similar to the following (assuming a Sales Header record has already been read):
"Sales Line".SETRANGE("Document Type","Sales Header...