Skip to main content
Community site session details

Community site session details

Session Id :

Using while select firstonly to avoid validations in Dynamics Ax

Community Member Profile Picture Community Member

When you execute a select statement to retrieve a single record most of us right the query first. This is followed by a validation to ensure that the query has returned a record. we make it simpler If we right it the way mentioned below…

static void WhileSelectInsteadoFSelect(Args _args)
{
    InventTable inventTable;
    ;
-----General way---------
    //fetch the record first
    select firstonly forupdate inventtable;

    //add an additional validation
    if (inventTable.recid)
    {
        inventTable.itemName = 'newname';
        inventTable.update();
    }

------Alternate--------------
    //prevents the extra 'if' chek
    while select firstonly forupdate inventTable
    {
        inventTable.itemName = 'newname';
        inventTable.update();
    }

    //can also be used for simple readonly
    while select firstonly inventTable
    {
        Info(inventTable.ItemId);
    }
}

A few of you might already follow it but for the rest it could be a learning :)



This was originally posted here.

Comments

*This post is locked for comments