Table method find() in D365 F&O - X++ Code
X++ Programming Language > D365 Table > Table Methods
4683
Answer:
All tables should have at least one find method that selects and returns one record from the table that matches the unique index specified by the input parameters. The last input parameter in a find method should be a Boolean variable.
Example Find Method
For example, let us look at the Find method that exists on the CustTable. In Visual Studio, go to the Application Explorer. Then, in the top bar, type CustTable, and hit enter. Located the node ‘CustTable’ under AOT>Data Model>Tables>CustTable. Right click on it, and select ‘Open designer‘. Immediately, a new window will open in the middle showing the table. First, we need to validate that there even exists a D365 Find method on this table. Many tables do not actually have a ‘Find‘ method. |
|
Next, expand the methods node. And scroll down until you find the ‘find‘ method. Right click on the node and select ‘View code’. In case you do not see the option for ‘View code’, right click on the very top table node labeled ‘CustTable’, and you should then see ‘view code’. Finally, a window will open with the x++ code. Locate the Find method. It will look like this. |
|
static CustTable find(CustAccount _custAccount, boolean _forUpdate = false) { CustTable custTable; if (_custAccount) { if (_forUpdate) { custTable.selectForUpdate(_forUpdate); } select firstonly custTable index hint AccountIdx where custTable.AccountNum == _custAccount; } return custTable; }
This is a static method named find
that searches the CustTable
table for a specific record based on the provided account number. Here's a breakdown of the code:
- The method signature specifies two parameters:
_custAccount
, which is the account number to search for, and_forUpdate
, which is a boolean value that determines whether the record is locked for update when it is retrieved (defaults tofalse
). - A
CustTable
variable namedcustTable
is declared. - An
if
statement checks whether_custAccount
is not null (i.e., whether an account number was provided). - If
_forUpdate
istrue
, theselectForUpdate
method is called oncustTable
to lock the record for update when it is retrieved. - The
select
statement is used to retrieve the first record fromCustTable
where the account number matches_custAccount
. Theindex hint
keyword is used to specify theAccountIdx
index to use for the search. This improves performance by telling the system to use a specific index rather than trying to find a suitable one. - The
custTable
variable is returned, which will contain the retrieved record if it exists or a null value if it does not.
So, in summary, this code provides a convenient way to search for a single record in the CustTable
table based on the account number. It also allows for the record to be locked for update if needed.
Finding The Unique Key
Now that you have seen an existing find method, it is time to create a new Find method on another table. Next, if you haven’t already create or find the table you wish to add a find method to. See How to create A Table In D365. As shown in the linked article, I created a table named TutCar. For the sake of this article, I will demonstrate using this table. But you can also use your own. First, search for ‘TutCar’ in the Application Explorer. Right click on the table, and select Open designer. |
|
Secondly, in the window that opens, expand the Fields, Indexes, and Methods nodes. I can see that this table does not already have a Find method. Therefore, I should add one. Also notice that there exists a unique index on this table, that contains the field ‘CarId‘. I can tell this because the ‘Allow Duplicates‘ property on the table is set to no. |
|
Given these points, when I create the D365 find method, I need to pass in a parameter that will filter on the CarId field. This will allow the system to find a single unique record. If my find method had passed in the description field, for example, this information would not allow me to find a unique record. Therefore, I need to always pass in whatever parameters will allow me to find a single unique record. |
Add A New Method
There are two ways of writing a method on a table. First, right click on the Methods node on the table, and select ‘New Method’. A code editor window will open with an empty method generated for you. |
|
Alternatively, right click on the table node, and select ‘View code’. Then begin typing your method in the code editor window that opens. |
|
Writing The Code, and update related code like below code. |
To write your own D365 find method, copying an existing method, and modifying it is usually the easiest thing to. Copy the CustTable find method written earlier in this article and paste it into the code editor window for your table. |
static TutCar find(TutCarId _carId, boolean _forUpdate = false) { TutCar tutCar; if (_carId) { if (_forUpdate) { tutCar.selectForUpdate(_forUpdate); } select firstonly tutCar where tutCar.CarId == _carId; } return tutCar; }
This Particular section is dedicated to Question & Answer only. If you want learn more about X++ Programming Language. Then you can visit below links to get more depth on this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.