What is find and exist method in AX 2012

Hi folks,

Select statement of x++ retrieves data from data from database but why then find method in ax 2012?

I would like to distinguish this one-by-one by using following snippet of code.

static InventTable find(ItemId itemId, boolean update = false)
{
InventTable inventTable;
;
inventTable.selectForUpdate(update);
if (itemId)
{
select firstonly inventTable
index hint ItemIdx
where inventTable.ItemId == itemId;
}
return inventTable;
}

1) of course, find method uses select statement to retrieve the data but it requires parameter
to search, if that exist, it searches otherwise it skips. that is one advantage of using find method. 
from example above, it accepts itemid as parameter, if it exist then it goes inside of if statement.

2) it accepts one more parameter i.e.,  boolean update which is false by default. Have a Boolean parameter with a default value of false. If true, the record should be selected for update.

3) Make the most effective select operation for the single record from the table and return it. Use an index that matches the required field, and use the firstOnly keyword (see the above example).

4) Have an optional parameter to enable the default Concurrency Model behavior specified on
the table to overruled. The default value for the parameter should be ConcurrencyModel::Auto. Usually, almost all the AX Tables have the OCCEnable Table property set to yes.
Optimistic Concurrency Control (OCC) helps increase database performance because the records are locked from the time when the actual update is performed.

5) you write exactly the same code as in find() method, then the behavior will be the same. The purpose of find() is that you don’t have to repeat it and method do more than just the select. Namely it doesn’t bother making any DB query if no value is provided and it has one or two extra parameters (_forUpdate and _concurrencyModel).

6) Run on both the client and the server. This is the default behavior. You can also make it explicit by putting “client server” in the declaration, but do not specify a single tier in the declaration (either the client or the server).

7) It is recommended to select a record from a table using this method based on primary key of the table, so that method returns only one record at a time.

7) find method is declared static because This method can be called from outside of the scope of table class.

exist method:
this method is similar to find() method but it returns boolean value specifying whether record is available as per parameter or not. This method is used to find whether a particular record exist in the the table or not. It requires the table primary key field as a parameter and returns true,if the record exists otherwise false.

for example:
static boolean exist(CustGroupId custGroupId)
{
return custGroupId
&& (select firstOnly RecId from custGroup
index hint GroupIdx
where custGroup.custGroup == custGroupId).RecId != 0;
}

Thank you and Happy Daxing…………..

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: