Always use recId to know if a select statement returns a record

In Axapta, there are two options to know if a select statement returns a record.
Option 1:
    select purchTable where
        purchTable.purchId == "Do not exist";
    if (purchTable)
    {
         …//Your logic here
    }

Option 2:
    select purchTable where
        purchTable.purchId == "Do not exist";
    if (purchTable.recId)
    {
         …//Your logic here
    }

It is strongly recommended to use recId. Because when working with aggregate functions in select statement. Table reference will always exist no matter a record is returned or not, but recId will be 0.
e.g.
    select count(purchId) purchTable where
        purchTable.purchId == "Do not exist";
    if (purchTable)
    {
        info("The record exists!");
    }

It will present the user "The record exist!" even there is no record returned.
This entry was posted in Axapta Development. Bookmark the permalink.

Leave a comment