rowCount() method on table buffers.

At first glance, this method looks like that it will return the number of rows selected on a table. But that is not the case. If we run the following lines, the result will be always -1

select custTable where custTable.custGroup=='10';
print custTable.rowCount();

The issue is that this method is to be used only with set based operations on the database. So, if you are using insert_recordset, update_recordset or delete_from, the rowCount() method will return the number of rows created, updated or deleted. Here is a sample job that shows this in action.

static void rowCountJob(Args _args)
{
CustTable custTable;
MyTable myTable;
;
ttsbegin;

insert_recordset myTable(AccountNum, PartyId, CustGroup, Currency)
select AccountNum, PartyId, CustGroup, Currency
from custTable
where custTable.CustGroup== '10';

// Prints 13 as 13 records with custGroup == 10 were inserted from CustTable
print myTable.RowCount();

update_recordset myTable
setting Currency='CNY'
where myTable.AccountNum like '11*';

// Prints 4 as 4 record exists( 1101, 1102, 1103, 1104)
print myTable.RowCount();

delete_from myTable
where myTable.AccountNum like '9*';

// Prints 1 as 1 record exists (9024)
print myTable.RowCount();

delete_from myTable;

// Prints 12 as 12 records are all that exists.
print myTable.RowCount();
ttscommit;

pause;
}

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

How to refer to form controls in code without setting the AutoDeclaration property in Dynamics AX?