Thursday, 6 July 2017

Number of ways to count the records

To get a table record count in AX 2012:------

1. To get a table record through count function:

In X++, how do you get a record count in table, I don't want to use while loop.?

Select a count on the RecId.
For example: 
CustTable   custTable;

select count(RecId) from custTable;

info(strfmt('Number of customers are: %1', custTable.RecId));

2. To get records through database.numberOfRowsLoaded()


    MainAccount             mainAccountLoc;
    CompanyInfo             companyInfoLoc;
    Int                               numOfUpdatedRec;
    MainAccountLegalEntity  mainAccountLegalEntityLoc;

    companyInfoLoc.clear();
    companyInfoLoc = CompanyInfo::findDataArea(FilterByLegalEntity.valueStr());

    mainAccountLoc.clear();
    mainAccountLoc = MainAccount_ds.getFirst(1);
    
        while(mainAccountLoc)
        {
            ttsBegin;
            select forUpdate mainAccountLegalEntityLoc
                where mainAccountLegalEntityLoc.MainAccount == mainAccountLoc.RecId
                &&    mainAccountLegalEntityLoc.LegalEntity == companyInfoLoc.RecId;
            mainAccountLegalEntityLoc.DefaultDimension = MainAccountLegalEntity.DefaultDimension;
            mainAccountLegalEntityLoc.update();
            ttsCommit;
            numOfUpdatedRec= MainAccountLegalEntity_ds.numberOfRowsLoaded();
            mainAccountLoc = MainAccount_ds.getNext();                       
        }
        info(strFmt('%1',numOfUpdatedRec));

3. To get updated/ inserted records through Integer counter:

    MainAccount             mainAccountLoc;
    CompanyInfo             companyInfoLoc;
    counter                       numOfUpdatedRec;
    MainAccountLegalEntity  mainAccountLegalEntityLoc;

    companyInfoLoc.clear();
    companyInfoLoc = CompanyInfo::findDataArea(FilterByLegalEntity.valueStr());

    mainAccountLoc.clear();
    mainAccountLoc = MainAccount_ds.getFirst(1);
    if (mainAccountLoc)
    {
        while(mainAccountLoc)
        {
            ttsBegin;
            select forUpdate mainAccountLegalEntityLoc
                where mainAccountLegalEntityLoc.MainAccount == mainAccountLoc.RecId
                &&    mainAccountLegalEntityLoc.LegalEntity == companyInfoLoc.RecId;
            mainAccountLegalEntityLoc.DefaultDimension = MainAccountLegalEntity.DefaultDimension;
            mainAccountLegalEntityLoc.update();
            numOfUpdatedRec++;
            ttsCommit;
            mainAccountLoc = MainAccount_ds.getNext();
        }
        info(strFmt('%1',numOfUpdatedRec));


4. X++ code to count records through Sysquery::countTotal() in query:

Following code illustrates how we can use SysQuery::countTotal() method to get the number of records in Query.

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


Happy DAXing....

No comments:

Post a Comment