Introduction to X++ Control Statements

Programming control statements are the source of many operations within Microsoft Dynamics 365 for Operations (MyDyn365).  This article explores four of the most popular statements that are used to manipulate and retrieve data.  In this case, we explore possibilities using a familiar tool, the Magic 8 Ball®.  The Magic 8 Ball (made by Mattel) is a mysterious device that provides solutions to all of life’s questions with a simple shake of the ball. Beneath the murky blue netherworld, there are many more choices than expected. The following classes use, the if (elseif) statement, the switch (case) statement, the select statement, and finally the while statement combined with the select statement to randomly answer questions from a list of values. The last two steps use a table to store the data set, which is typical in MsDyn365.  For each example, the common methods are used for each of these classes.  The print method, which often acts as the get, is illustrated for each case to show the differences in the programming methodology.

Setup

The first part sets up the Eightball class so that we have a common environment for the individual methods.  They cover the basics of the classDeclaration, new, main, and introMessage.  It also includes the instructions on how to create the table for the while select classes.  See my article on Database Manipulation for the process of populating the table with an insert statement.

Class Setup

  1. Open Visual Studio as an administrator and create a package, model, and project to contain your objects. I used CrazyLucky and CrazyLuckyProject for the model name and project name respectively.
  2. Insert the code shown below.

control1

control2

Create the Table

  1. Open Visual Studio as an administrator and create a package, model, and project to contain your objects. I used CrazyLucky and CrazyLuckyProject for the names.
  2. Right-click the project and select Add > New Item.
  3. Select Data Model > Table.
  4. Enter CrazyLuckyTable for the name and click Add.
  5. From the Application Explorer, under the Data Types node, expand Extended Data Types.
  6. Locate the Name element and drag it to the CrazyLuckTable
  7. Locate the Integer element and drag it to the CrazyLuckTable
  8. Save all and click Build.
  9. Ensure that there are no errors. If you have an error resolve it and rebuild.

control3

Here are the possible answers from the Magic Eight Ball:

It is certain It is decidedly so Without a doubt Yes, definitely
You may rely on it As I see it, yes Most likely Outlook good
Yes Signs point to yes Reply hazy try again Ask again later
Better not tell you now Cannot predict now Concentrate and ask again Don’t count on it
My reply is no My sources say no Outlook not so good Very doubtful

The If Statement

One of the oldest and most used programing language items is the if statement. It appears in classic languages, logic trees, databases, and even simple spreadsheets. The if statement for X++ supports all of this functionality including the elseif. The following class method exemplifies this.

If Statement Process

  1. Open the Eightball class.
  2. Insert the method shown below.
  3. Uncomment line 22.

control4

  1. Save all and click Build.
  2. Ensure that there are no errors. If you have an error, resolve and rebuild.
  3. Right-click Eightball and set it as Startup Object.
  4. Click Start on the menu.
  5. Observe the results.
  6. Click Stop on the menu.

Switch case statement

When it comes to database design, switch case often shows up as a better choice for a list. In this case, it uses much the same logic as the if statement; however, it accomplishes the task in fewer characters and the code is easier to read. Also, in this example, there is an answer set to a label, which is a much better design because the label can be translated to a multitude of language types supporting global Dynamics 365 for Operations implementation.

Switch Case Process

  1. Open the Eightball
  2. Insert the method shown below.
  3. Comment line 22.
  4. Uncomment line 23.control5
  5. Save all and click Build.
  6. Ensure that there are no errors. If you have an error, resolve and rebuild.
  7. Click Start on the menu.
  8. Observe the results.
  9. Click Stop on the menu.

Select Statement

The select statement allows us to be able to find in the table answernumber and answer where it matches luckyNumber. This is typical of programming in X++ for Dynamics 365 for Operations because the data is in tables and invites the use of grouping, filters, and joins for much more complex data manipulation.

Select Process

  1. Open the Eightball
  2. Insert the method shown below.
  3. Comment line 23.
  4. Uncomment line 24.control6
  5. Save all and click Build.
  6. Ensure that there are no errors. If you have an error, resolve and rebuild.
  7. Click Start on the menu.
  8. Observe the results.
  9. Click Stop on the menu.

While statement

You can use the while statement to repeat in a loop format over the contents of the table in order to find the appropriate record, because it may be unknown. This is much more likely in Dynamics 365 for Operations programming because it may possibly be manipulating millions of records. This version of the while statement is called the do while. It executes the loop at least once. An alternative method is to use the while method that evaluates at the top of the equation, so it may not run. On a quick programming note, the counter variable needs to be declared in the class declaration, and the initial value is 0 (zero).

While process

  1. Open the Eightball class
  2. Insert the method shown below.
  3. Comment line 24.
  4. Uncomment line 25.control7
  5. Save all and click Build.
  6. Ensure that there are no errors. If you have an error, resolve and rebuild.
  7. Click Start on the menu.
  8. Observe the results.
  9. Click Start on the menu.

Conclusion

This article explored the key aspects of four X++ programming control statements in MsDyn365. Through the use of the Magic 8 Ball, you randomly selected a statement that improves business decision making with wisdom from beyond. The If statement shows the logic tree that can be used to evaluate a statement and assign a value or execute an operation.  The switch statement explores much of the same information used in the if statement, only in less characters.  The select statement gives the opportunity to see the data retrieval that is possible.  Finally, the while statement adds a looping structure for the review, update, or deletion of millions of records in a table.  This is not a comprehensive approach to all that X++ programming uses for control statements; however, it describes the basics that enable further pursuit to working with MsDyn365.

For more information I would recommend that you attend our Dynamics AX or Dynamics 365 training in person or on-demand online to learn more.

Plus, keep an eye out for more articles that dive in to the technical aspects of using X++ code.  Visit academy.rsmus.com for more information and training materials that will cover this and hundreds of other topics for AX2012 and MsDyn365.

by Shaun McMikle for RSM