Count records in Query Vs Cound Loops in Query

Count records in Query Vs Cound Loops in Query

MCP · Azure DevOpsYour delivery board, readable by an agentMCP · DataverseOne consent, and the data layer opensMCP · Dynamics 365 F&OConnecting F&O to an agent Microsoft hasn’t documented
Sometime we do need to know the number of records fetching through X++ query and this can be achieved by SysQuery::CountRecords(QueryRun) function. 

This function works similar to this X++ logic;

ProjTable   projTable;    
select count(RecId) from projTable;info(strFmt("%1 total records", projTable.RecId)); // total tecords 1130

Lets see with X++ query;

static void CountProjTableRecords(Args _args)
{      
    Query                           query = new Query();    
    QueryRun                     queryRun;    
    QueryBuildDataSource qbd;    
    qbd = query.addDataSource(tablenum(ProjTable));
   
    queryRun = new QueryRun(query);
   
    info(strfmt("Total Records in Query %1", SysQuery::countTotal(queryRun))); 
// total tecords 1130
}

However, this SysQuery::CountRecords(QueryRun) function count only the number of records of the first datasource. Let's assume if we need to count number of records of a query with more than one datasource, we need to use SysQuery::CountLoops(QueryRun) function.

static void CountProjTableRecords(Args _args)
{      
    Query                           query = new Query();    
    QueryRun                     queryRun;    
    QueryBuildDataSource qbds, qbds1;    
    qbds = query.addDataSource(tablenum(ProjTable));
    
    qbds1 = query.addDataSource(tablenum(ProjTable)).addDataSource(tablenum(ProjForecastTable));    
    queryRun = new QueryRun(query);
   
    info(strfmt("Total Records in Query %1", SysQuery::countLoops(queryRun))); 
}
Previous article← Show last day of the month from date in SSRS report
Discussion

Ask a question or leave feedback

This is a review interaction. The WordPress version will store, thread and moderate comments.

Thanks — submitted for moderation in this review.
Search the whole site · Ctrl/⌘ + K