Microsoft Dynamics 365: Update Dangers Using X++ Code

By - August 29, 2017

The programming for update statements has quite a punch in X++ code for Microsoft Dynamics 365 for Finance and Operations (MsDyn365). In a previous blog posts we covered the insert and update process for the creation data. In this article, we will explore both the danger and the power of the update statement using the Invent Item Price Tolerance group that can be found in every released product. Our emphasis is on the responsibility of programmers (both novice and experienced) to understand the behavior of the code that we write and to continue to enforce database integrity rules. Specifically, we will review where to find the fields in the user interface and how to populate the table. Then we will run a dangerous update process and review the results. As a technical note, your programming model will need to reference the Directory and Ledger packages in order to run the Update function.

Finding the Item Price Tolerance Group Field

The goal of this exercise is to watch the interaction between fields, tables, and update statements.  In order to do this, we first need to find the item price tolerance group. This is hidden away in the Released products form.

To navigate to the item price tolerance group field:

  1. In the USMF company, click Show navigation pane.
  2. Open Product Information Management > Products > Released products.
  3. Select T0002 Projector Television.
  4. Expand the Purchase FastTab if necessary. 
  5. Click Edit.
  6. Click the drop-down menu to verify that there are no values in the Item price tolerance group form/table.

Populating the Table Using Insert
This next process populates the table. We are using an Insert statement with the goal to add two values, one for 4% and the other for 6%.  Once we have inserted the values, we will verify that they are available.

To insert and validate the initial values:

  1. Insert the values to the table.
  2. Run Visual Studio as an administrator.
  3. Open your current project in your current model. If no projects are configured, see my previous blog posts for the procedure.
  4. Right-click the project and select Add > New Item.
  5. Select Code > Runnable Class (Job).
  6. Enter rsmSampleInsert in the Name
  7. Click Add.
  8. Enter the following code:
  9. Save all.
  10. Set the current project as Startup Project.
  11. Set the rsmSimpleInsert class as the Startup Object.
  12. Build the project.
  13. Click Start to run the debugger.
  14. Once inserted, the info log will read Yahoo!
  15. Close the browser.
  16. Stop the debugger.
  17. Check the values in the interface.
  18. In the USMF company, click Show navigation pane. You may need to refresh the browser to relink to the current image.
  19. Open Product Information Management > Products > Released products.
  20. Select T0002 Projector Television.
  21. Expand the Purchase FastTab if necessary.
  22. Click Edit.
  23. Select 4% as the item price tolerance.


Dangerous Update Process
In this step, we will be using the dangerous update process.  As you will see, our code attempts to change the 4% value of the field in the Released products table to 7%, a value that is not listed in the item price tolerance group. The assumption of the model is that, as a programmer, you know what you need the value to be and you are running that process now to change it.

To run the update statement:

  1. Run Visual Studio as an administrator.
  2. Right-click the project and select Add > New Item.
  3. Select Code > Runnable Class (Job).
  4. Enter rsmSampleUpdate in the Name field and click Add.
  5. Enter the following code:
  6. Set the current project as Startup Project.
  7. Set the rsmSimpleInsert class as the Startup Object.
  8. Build the project.
  9. Click Start to run the debugger.
  10. Once inserted, the info log will read this works! It will also show that there will be seven updates.
  11. Close the browser.
  12. Stop the debugger.

Reviewing the Results
Finally, we are going to review the results of our update statement. The goal was to change the value of the item price tolerance group on the released products from 4% to 7%.  We did this, but the process disregards the values within the Item price tolerance group table that we inserted previously.

To check the results:

  1. Refresh the browser. This will show the 7% item price tolerance group.
  2. Click Edit in the action pane.
  3. Click the Item price tolerance group drop-down arrow to review the contents. You will see a listing for 4% and 6%!

Conclusion
This article explored the process of where to find the item price tolerance group field and form in the user interface, how to populate the table, and then run a dangerous update process, and finally review the results. This is a simplification of the update process and the dependency of fields on a form that illustrates the dangers of programmer work that can change large numbers of fields and functionalities in Dynamics 365 for Finance and Operations with unintentional results. In our case, we populated the table to two possible values, 4% and 6% and using the update statement we were able to override to a number that wasn’t even possible. The goal is to give you insight into the power of the X++ code on your data, forms, and tables.

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 into the functional and technical aspects of X++ coding.  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

Receive Posts by Email

Subscribe and receive notifications of new posts by email.