Counting the number of records in a QueryRun

static void countRecords(Args _args)
{
    Query                   q = new Query();
    QueryBuildDataSource    qbdsInventTable,
                            qbdsInventDimCombination;
    QueryRun                qr;
    Integer                 counter;
    Integer                 loops;
    Common                  common;
    
    qbdsInventTable = q.addDataSource(tableNum(InventTable));
    qbdsInventTable.addSelectionField(fieldNum(InventTable, RecId), SelectionField::Count);
    
    qr = new QueryRun(q);
    
    // Simple way to get record count from Query/QueryRun class
    counter = SysQuery::countTotal(qr);
    //loops   = SysQuery::countLoops(qr);

    // Via while
    counter = 0;
    loops   = 0;
    while (qr.next())
    {
        common  = qr.get(q.dataSourceNo(1).table());
        counter += common.RecId;
    }
    
    /*
    Second variant might be a lot more efficient and accurate if you have several datasources joined as standard countTotal also tries to count loops
    Adding joined datasource for you to checkout the difference
    */
    
    qbdsInventDimCombination = qbdsInventTable.addDataSource(tableNum(InventDimCombination));
    qbdsInventDimCombination.relations(true);
    
    qr = new QueryRun(q);
    
    // Way to get record count using SysQuery
    counter = SysQuery::countTotal(qr);
    //loops   = SysQuery::countLoops(qr);
    
    // Via while
    loops   = 0;
    counter = 0;
    while (qr.next())
    {
        common  = qr.get(q.dataSourceNo(1).table());
        counter += common.RecId;
    }
}


Support The Author

 If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.

Buy Me a Coffee

Post a Comment


All Comments


No comments. Be the first one to comment on this post.

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own "buy me a coffee" link.
Join us.

Blog Tags