Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to get the customer opening balance using X++

(0) ShareShare
ReportReport
Posted on by 1,737

Hi,

I am trying to write job  to display an Infolog of all customers with an open balance greater than 5,000 in the customer currency and display the balance in the customer and master company currency.

How to get this.

regards,

Srinivas Pamidi

*This post is locked for comments

  • Brandon Wiese Profile Picture
    17,788 on at
    RE: How to get the customer opening balance using X++

    Easy enough to fix.  Just use CustTrans instead of CustTransOpen, and filter on TransDate <= Assessment date.

  • Mea_ Profile Picture
    60,278 on at
    RE: How to get the customer opening balance using X++

    @Brandon

    That's faster if we don't  need  Assessment Date, because you dropped a half of the method.

  • Verified answer
    Brandon Wiese Profile Picture
    17,788 on at
    RE: How to get the customer opening balance using X++

    This is even faster using a single set based operation, but still uses a CrossCompany query.

        Query                       q;
        QueryBuildDataSource        qbds;
        QueryRun                    qr;
        QueryHavingFilter           qhf;
        CustTable                   custTable;
        CustTransOpen               custTransOpen;
        ;
        
        q = new Query();
        q.allowCrossCompany(true);
        
        qbds = q.addDataSource(tableNum(CustTable));
        
        qbds.addGroupByField(fieldNum(CustTable, DataAreaId));
        qbds.addGroupByField(fieldNum(CustTable, AccountNum));
        
        qbds = qbds.addDataSource(tableNum(CustTransOpen));
        qbds.relations(true);
        
        qbds.addSelectionField(fieldNum(CustTransOpen, AmountMST), SelectionField::Sum);
        qbds.orderMode(OrderMode::GroupBy);
        
        qhf = q.addHavingFilter(qbds, fieldStr(custTransOpen, AmountMST), AggregateFunction::Sum);
        qhf.value('> 5000');
            
        qr = new QueryRun(q);
        while (qr.next())
        {
            custTable = qr.get(tableNum(CustTable));
            custTransOpen = qr.get(tableNum(custTransOpen));
            
            info(strFmt("Company: %1, Account number: %2, Open balance: %3", custTable.dataAreaId, custTable.AccountNum, custTransOpen.AmountMST));
        }


  • Verified answer
    Brandon Wiese Profile Picture
    17,788 on at
    RE: How to get the customer opening balance using X++

    This is faster than using the openBalance method in a CustTable loop.

    The disadvantage of a CrossCompany query is that users will only see customers for which they have access.  Looping through DataArea and using changeCompany(..) ignores user security.

        CustTable                   custTable;
        CustTransOpen               custTransOpen;
        ;
        
        while select crossCompany DataAreaId, AccountNum
            from custTable
            group DataAreaId, AccountNum
                join sum(AmountMST)
                from custTransOpen
                where custTransOpen.AccountNum == custTable.AccountNum
        {
            if (custTransOpen.AmountMST > 5000)
            {
                info(strFmt("Company: %1, Account number: %2, Open balance: %3", custTable.dataAreaId, custTable.AccountNum, custTransOpen.AmountMST));
            }
        }


  • Verified answer
    Mea_ Profile Picture
    60,278 on at
    RE: How to get the customer opening balance using X++
    Try this:

    DataArea dataAread; CustTable custTable; while select id from dataAread where !dataAread.isVirtual { changecompany(dataAread.id) { custTable = null; while select custTable { if (custTable.openBalanceCur() > 5000) { info(strFmt("%1,%2,%3", custTable.AccountNum, custTable.openBalanceCur(),custTable.Currency)); } } } }
  • srinivas pamidi Profile Picture
    1,737 on at
    RE: How to get the customer opening balance using X++

    Now it is working changing code like this

    while select custTable

      {

       if (custTable.openBalanceCur() > 5000)

           {

           info(strFmt("%1,%2,%3", custTable.AccountNum, custTable.openBalanceCur(),custTable.Currency));

           }

    }    

    but how to run this job in cross company?

  • srinivas pamidi Profile Picture
    1,737 on at
    RE: How to get the customer opening balance using X++

    Hi

    i am writing like this but info log is not open

    while select custTable

      {

       if (custTable.openBalanceCur(fromDate, toDate, assessmentDate, custTable.Currency) > 100)

            {

           info(strFmt("%1,%2", custTable.AccountNum, custTable.openBalanceCur()));

           }

    }

  • Suggested answer
    Mea_ Profile Picture
    60,278 on at
    RE: How to get the customer opening balance using X++

    Hi srinivas,

    You can try something like

    while select custTable
    {
        if (custTable.openBalanceCur(froamDate, toDate, assessmentDate, custTable.Currency)) > 5000)
             {
                  //info();
             }
     }


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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,217 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,978 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans