Skip to main content

Notifications

Find method and its usage

First, we will understand what this find method is and why is this important in D365 FO.

For storing all the data in D365, we have a database where all data will be stored as records in backend tables. Most of the times we need to pick this data, edit or do something with this data in backend. We may create reports also. So, such times we will use select statement or a find method to retrieve the data.

So, what this find method is?

The find method is a static method at table which is used instead of a select statement in X coding. For example, to select a single record from VendTable, we will give the following select statement.

VendTable     vendTable;

select firstonly AccountNum from vendTable where vendTable.AccountNum == “Vend1”;
info(strFmt(“%1”, vendTable.AccountNum));

'firstonly' keyword as we have a single record for that particular ID and selecting only AccountNum as we require only that field to show as a info statement.

So, the above select statement returns that record where AccountNum is “Vend1”. 

This can also be done by using below find method.

VendTable  vendTable = VendTable::find(“Vend1”);
info(strFmt(“%1”, vendTable.AccountNum));

This will also return the same thing. We can use any of them.

But if we require just few fields then I would recommend using normal select statement explicitly mentioning each and every field. Because the find method will retrieve data from all the fields. If all the fields’ data is not required, then it is not necessary to use find method at such cases.

Now how to write the find method in any custom table?

You can simply copy any standard table find method and use that in your code as below.

Below is the find method for VendTable.

public static VendTable find(
        VendAccount             _vendAccount ,
        boolean                 _forupdate        = false,
        ConcurrencyModel        _concurrencyModel = ConcurrencyModel::Auto)
    {
        VendTable vendTable;

        vendTable.selectForUpdate(_forupdate);
        if (_forupdate  && _concurrencyModel != ConcurrencyModel::Auto)
        {
            vendTable.concurrencyModel(_concurrencyModel);
        }

        if (_vendAccount != '')
        {
            select firstonly vendTable
                where vendTable.AccountNum == _vendAccount;
        }

        return vendTable;
    }

This method can be copied into your table method and should be changed accordingly like in the name of VendTable, your custom table name and in the place of VendAccount, your index field name. 

Now the use of find method is, as we have 2 parameters where 2nd parameter is optional or default,

  • If you just give the Vendor account number, then it will retrieve the only record with that vendor account number taking into consideration the other parameter _forupdate is false. 
  • If you give both vendor number and _forupdate to true, then it will retrieve the record and update the record as well.
  • The unique index can be found simply by looking at the find method as the mandatory parameters in find method are unique.

By this we can understand that a find method can be used to retrieve the data or update the data. We can also use this for finding the unique index of the table.

This is also best practice to include find method in the table.

Comments

*This post is locked for comments