As I’ve progressed as an AX developer, I’ve had to lean on many of the skills that I had from a former job as a C# developer where I used a lot of SQL queries. Working with data in X++ is similar until you try to write it like it would be written in SQL.
I would like to explain a quick tip that I got when working with multiple joins recently in X++, as well as some other best practices when working with data.
Working with vendTrans and ProjTable, I needed to join a few tables to get to the data that I needed. As I stated above, I came from a very SQL query heavy development environment, so my first step when working with data like this is to write it in SQL.
|select p.Name, p.Workerresponsible, p.custAccount, v.ProjId, vit.*,|
from PSAPwpInvoiceTransView as v
inner join VendInvoiceTrans as vit
on vit.RecId = v.VendInvoiceTransRecId
inner join VendTrans as vt
on vit.InvoiceId = vt.invoice
inner join ProjTable as p
on p.ProjId = v.projId
where vt.voucher = ‘#########’
First X++ select:C#
join ProjId from view
join Name, WorkerResponsible, CustAccount from projTable
where projTable.ProjId ==view.ProjId
&& vendTrans.voucher == ‘#########’
&& vendInvoiceTrans.InvoiceId == vendTrans.invoice
&& vendInvoiceTrans.RecId == view.VendInvoiceTransRecId
As you can see above, I have all of the same joins and fields selected. There are definite differences in the languages that you cannot get around, but the tip that I received allows you to better see and understand were your joins are and where you might have an issue.
Easier to read X++:C#
|select firstonly vendTrans|
where vendTrans.voucher == ‘#########’
exists join vendInvoiceTrans
where vendInvoiceTrans.InvoiceId == vendTrans.invoice
exists join view
where vendInvoiceTrans.RecId == view.VendInvoiceTransRecId
exists join projTable
where projTable.ProjId ==view.ProjId
More X++ Select Statement Tips:
1. Place the where clauses for each join under the line adding the join and table.
This gives you a better view of which fields are being joined, and helps with debugging if you have issues. This also give you a more “SQL-Like” visual of the select statement.
2. Make sure that you are using the correct joins.
As seen above, I had inner joins on all of the tables and was selecting fields that I thought I needed. After some testing, I realized the best practice is using Exists Joins, as then I did not need the fields from the joining tables and I only needed to make sure they existed in the table. This speeds up the select as well as returns only the data that you need from VentTrans.
3. Use field groups to select ONLY the values that you need.
As seen in the first X++ select that I wrote, I added a few of the fields that I needed from each table. Example:C#
|join Name, WorkerResponsible, CustAccount from projTable|
This will return only the fields stated, whereas without calling them out you would get all fields from the projTable.
4. First only and First fast.
In the second select code block, I added the firstOnly directive. This directive speeds up the data retrieval by getting the first record that it needs. FirstFast was not a great solution to this as it may still incur overhead setting up the cursor to support additional results. Therefore, if you know there is only a single row, use First only.
- Conditional ‘Where’ Clauses in ‘Select’ Statements in Dynamics AXEver found yourself in a situation where you conditionally want to apply a ‘where’ clause in a select statement in Dynamics AX? Here’s how you can do it: There are…
- Creating Cross Company Select Statements in X++When creating a query in X++ by default that query will only return the values in the company that you are logged into. If you desire to pull from multiple…
- Microsoft Dynamics AX – Option for More Flexible ‘Where Clause’ in Select StatementsWhen working on a Dynamics AX R3 upgrade I noticed some code around a ‘where clause’ that I had not seen before. I discussed it with some other team members…