X++ code to get sales order totals in D365FO
Below is the code snippet to get sales totals of open order and invoiced sales orders
Open order Sales Orders:
container displayFields = SalesTotals::displayFieldsServer(salesTable, SalesUpdate::All, "");
real totalAmountIncTax = conpeek(displayFields, TradeTotals::posTotalAmount());
real totalAmountExcTax = conPeek(displayFields, TradeTotals::posBalance());
real totalAmountTax = conpeek(displayFields, TradeTotals::posTaxTotal());
real totalAmountIncTax = conpeek(displayFields, TradeTotals::posTotalAmount());
real totalAmountExcTax = conPeek(displayFields, TradeTotals::posBalance());
real totalAmountTax = conpeek(displayFields, TradeTotals::posTaxTotal());
Invoiced Sales Orders:
SalesQty qty;
Amount totalAmountIncTax,totalAmountTax,totalAmountExcTax;
[totalAmountIncTax,totalAmountTax,totalAmountExcTax,qty] = ANI_SalesEventHandlers::getSalesInvoiceAmountAndTax(salesTable);
real totalAmountExcTax = totalAmountExcTax;
real totalAmountIncTax = totalAmountIncTax;
real totalAmountTax = totalAmountTax;
/// <summary>
/// Get Invoiced Sales Order Amount, tax, subtotal amount and Qty.
/// </summary>
/// <param name = "_salesTable">The <c>SalesTable</c> buffer.</param>
/// <returns>Container with sales order totals.</returns>
public static container getSalesInvoiceAmountAndTax(SalesTable _salesTable)
{
CustInvoiceJour custInvoiceJour;
Amount invoiceAmount,taxAmount,salesBalance;
SalesQty qty;
while select SumTax, InvoiceAmount, CurrencyCode, SalesId, SalesBalance, Qty from custInvoiceJour where custInvoiceJour.SalesId == _salesTable.SalesId
{
invoiceAmount += custInvoiceJour.InvoiceAmount;
taxAmount += custInvoiceJour.SumTax;
salesBalance += custInvoiceJour.SalesBalance;
qty += custInvoiceJour.Qty;
}
return [invoiceAmount,taxAmount,salesBalance,qty];
}