AX Developer Tips for Newbies: Part 2

by | Updated August 15, 2016 | Development, Dynamics AX

In a previous post I compiled a collection of tips gleaned from teaching AX developers in my training classes. The selections included in AX Developer Tips: Part 2 are  dedicated to X++ coding tips.

Read more on AX Developer Tips for Newbies: Part I here. 

 

Select statements

A. It is highly recommended to use a field list in your Select statements if you do not need access to all fields in the table. Just remember, only the fields in the list will contain values. If you try using another field later in your code, you will get unexpected results.

B.  Similarly, the exists/notexists join returns no data at all. In the following statement, the custInvoiceJour record buffer will contain no data.

 

For this reason, it is not possible to join another table using custInvoiceJour. This won’t work. The custTrans buffer will be empty.

 

Where Clause and Indexes

When issuing select statements against tables in X++ code, align the where clauses in the select statements to the fields that are contained within an existing index. If this is not possible, then consider adding an index to the table that matches the where clause. Where statements that do not align with indexes can cause table scans which lead to significant performance problems when the tables have a large number of records in them. This is especially true when selecting records from transactional tables.

Avoid statements like the following where the LedgerJournalTrans table does not have an index that contains the AcknowledgementDate in it.

 

 

FirstOnly

In select statements use the keyword firstonly to retrieve the first record found in the database table when verifying that a condition is true or false. For example when verifying if a given sales order contain any sales lines, firstonly can be used because once the first sales line is found the condition is true. Also use firstonly when the developer knows that only one record will be found in the table. By using the keyword the search through the database is completed as soon as the first record is identified.

 

Replace this code:

With:

 

Transactions and Locking

A.  When there are ttsbegin and ttscommit statements nested within other ttsbegin and ttscommit statements, locks held on records in the table are not released until the outer ttscommit is reached, so keep the number of lines of code between the ttsbegin and ttscommit as small as possible. Additionally since the locks are not released until the ttscommit is reached avoid any tts statements that wrap dialogs requiring user intervention.

B. In the following code locks are initiated in both of the update statements. Those locks are not released until the final/outer ttscommit statement is reached:

 

Table Methods

Before writing a lot of code to access or update base tables in AX, take a look at the methods on the table you are interested in. Most tables in AX have built-in methods you can use to perform basic tasks like finding and initializing records.

Examples:

Find method – find a record by the primary key

 

InitFrom methods -Initialize fields in the table from another table

 

 

Cross-reference

Before anything else, do yourself a favor and update the AOT cross-reference in your development environment. The cross-reference system allows you to see the relationships between objects. It is most useful for finding where EDT’s, fields or methods are used in AX. When you are learning, it is an invaluable tool for quickly finding code examples in the base product and also ensures that you adhere to standard coding practices.

Yes, the update takes a long time to run. Try running it from a Job instead of from the Tools menu. Here is a sample job. Start it up at the end of your work day and it will be finished by the next morning.

 

Once the job is complete, you will have the Used By option as below:

AX Dev Tips

 

We can find out how the ChooseLines method is used and from where:

Choose Line Method in AX

 

Best Practices Compiler

Set the Compiler to Level 4 when you are writing new code. AX will point out Best Practice deviations at compile time.

Best Practice Compiler

 

Under Tools, Options, Development Tab, select Best Practices. Here you can set the checks that you want to perform.

Best Practices in Dynamics AX

 

 

Related Posts

  • I have compiled a collection of tips that seem to help new AX developers in my training classes, so I thought it would be good to share with the public.…

  • Over on the AX Support site on MSDN, Daniel Durrer has taken the time to keep tabs on the latest builds for Microsoft Dynamics AX 4.0, 2009 and 2012.  I…

  • TFS can be a wonderful tool for the Dynamics AX Developer to help track bugs, tasks, etc. If it is used correctly, it can greatly increase your productivity. To learn…

0 Comments

Trackbacks/Pingbacks

  1. AX Developer Tips for Newbies: Part 1 | Stoneridge Software Inc - […] Read more on X++ coding tips in AX Developer Tips for Newbies: Part 2 […]

Submit a Comment

Your email address will not be published. Required fields are marked *

Upcoming Events

october

07oct12:00 pm1:00 pmThe Three Paths to Business Central from Dynamics GP

08oct11:00 am12:00 pmConfab with Stoneridge - Livestream - The Vision and Strategy of Microsoft Business Systems

14oct10:00 am10:30 amThe Modern Manufacturer - Managing Complex Cost Modeling

14oct12:00 pm12:30 pmGenerating Custom Inspection or Process Forms

19octAll Day22Stoneridge Connect Fall 2020

22oct11:00 am12:00 pmConfab with Stoneridge - Livestream - Stoneridge Connect Recap

28oct10:00 am10:30 amThe Modern Manufacturer - Engineering Change Management: Introduction of NEW Functionality for Manufacturers Using Dynamics 365

november

11nov10:00 am10:30 amThe Modern Manufacturer - Tears and Trauma of MRP

About Stoneridge
Stoneridge Software is a unique Microsoft Gold Partner, with emphasis on partner. With specialties in Microsoft Dynamics 365, Microsoft Dynamics AX, Microsoft Dynamics NAV, Microsoft Dynamics GP and Microsoft Dynamics CRM, we focus on attracting the most knowledgeable experts in the field to our team, and prioritize delivering stellar solutions with maximum impact for your business. At Stoneridge, we are deeply committed to your results. Each engagement is met with a dedicated team, ready to provide thorough, tailored, and expert service. Based in Minnesota, we intentionally “step into your shoes,” wherever you are. We focus on what you care about, and develop trusting, long-term relationships with our clients.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!

X