Calculating Customer Aging Balances in X++

By Laura Lake | May 23, 2017

I was recently asked to add an Account Summary to the footer of a report in AX 2012, to look something like this:

CustomerAgingBalances_LauraLake

In my research, I came across a helpful class that allowed me to meet this requirement of calculating customer aging balances in X++ with a minimum of coding effort; the CustVendAgingStatistics class. The calcStatistic method queries StatRepInterval and StatRepIntvervalLine to get the buckets and settings for the calculation, then it writes out the values to tmpAccountSum, which is an InMemory temporary table. Depending on what aging buckets you need, you can use a debugger to figure out which tmpAccountSum records you will want.

For my purpose, I created a new method to calculate the customer’s aging balances using a 30D format (#agingFormat macro). I needed open balances (Balance03) and I opted to store both balance and description in a Map. The integer key was helpful so that I could retrieve the values later and add them to the SalesInvoiceHeaderFooterTmp record in SalesInvoiceDP.insertSalesInvoiceHeaderFooterTmp method. You could easily use a container here, depending on how you are using the aging information.

private void SSI_calcAging(CustInvoiceAccount _invoiceAccount)
{
    CustVendTable               custVendTable;
    CustVendAgingStatistics     custVendAgingStats;
    TmpAccountSum               tmpAccountSumLocal;
    int                         i;
    custVendTable  = CustTable::find(_invoiceAccount);

    custVendAgingStats =       CustVendAgingStatistics::construct(custVendTable,#agingFormat,
DateTransactionDuedate::DueDate,True);
    custVendAgingStats.calcStatistic();
    tmpAccountSumLocal.setTmpData(custVendAgingStats.tmpAccountsum());
    i = 0;
    cachedaccountSummary = new Map(Types::Integer,Types::Real);
    cachedaccountSummaryTxt = new Map(Types::Integer,Types::String);
    while select tmpAccountSumLocal
    {
        i++;
        cachedaccountSummary.insert(i,tmpAccountSumLocal.Balance03);
        cachedaccountSummaryTxt.insert(i,tmpAccountSumLocal.Txt);
    }

}

Obviously, this could be used for Vendor Aging as well. I verified that the values match exactly with the Customer Aging report when it is run with the same parameters.

Laura Lake
Our Verified Expert
Laura Lake

Laura Lake is a seasoned application developer with more 20 years of experience, specializing in Dynamics NAV and AX. She provided advanced developer support as an Escalation Engineer during her time at Microsoft, and later took on her role at Stoneridge Software leading client customizations and development workshops.
Her recent efforts include architecting tailored development solutions, guiding project teams through complex technical builds, and mentoring clients on extensibility within the Dynamics ecosystem. With a passion for robust, maintainable design, Laura brings expert craftsmanship and thoughtful leadership to every project.

Read More from Laura Lake

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!