web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

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

I have the same question (0)
  • Suggested answer
    Mea_ Profile Picture
    60,286 on at

    Hi srinivas,

    You can try something like

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


  • srinivas pamidi Profile Picture
    1,737 on at

    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()));

           }

    }

  • srinivas pamidi Profile Picture
    1,737 on at

    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?

  • Verified answer
    Mea_ Profile Picture
    60,286 on at
    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)); } } } }
  • Verified answer
    Brandon Wiese Profile Picture
    17,788 on at

    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
    Brandon Wiese Profile Picture
    17,788 on at

    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));
        }


  • Mea_ Profile Picture
    60,286 on at

    @Brandon

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

  • Brandon Wiese Profile Picture
    17,788 on at

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Sagar Suman Profile Picture

Sagar Suman 2 Super User 2026 Season 1

#1
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#1
Pratik Bhosle Profile Picture

Pratik Bhosle 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans