Chapter 6. Manipulating Data

Throughout Dynamics AX, you can find data manipulation in code with the insert(), update(), and delete() methods, for example, when posting an invoice. When this is done, the system has to create records in journals and update data in different tables based on the data that already exist in an order and in other base data tables.

In this chapter, we will look at the following topics:

  • The validation methods used to validate the data to be inserted/updated/deleted
  • The insert() method used to insert data
  • The update() method used to update existing data
  • The delete() method used to delete records
  • The insert_recordset operator used to send a request to the database to insert a chunk of data
  • The update_recordset operator used to send a request to the database to update a chunk of records
  • The delete_from operator used to send a request to the database to delete a chunk of records
  • Direct handling for direct data manipulation without running any code within the update, insert, or delete methods
  • Unit of work as a new framework in AX 2012 that allows for bulk/mass data create/update operations

The validation methods

Before manipulating the data, it's a good idea to make sure that the data you are inserting, updating, or deleting doesn't break any of the rules you have set for the table you are manipulating.

These rules are typically to check that all mandatory fields have data in the buffer that you are trying to insert or update, or to check that the record you are about to delete doesn't have any related records in the tables set up with a restricted delete action.

A validation method should always return a Boolean value. This value will be true if the validation is OK and false if it's not OK.

If you don't use the validation methods, you will be allowed to have records with mandatory fields empty when inserting or updating records from X++. You will also be allowed to delete records that have restricted delete actions against other tables. This means that you are likely to get unreferenced data if you don't use validation methods.

There are three data validation methods used in tables and data sources in AX forms and they all have default functionality defined in the system class Common available when they call super():

  • validateWrite: This method validates records to be inserted or updated and checks for mandatory fields. This method's data is automatically triggered when a user writes or updates data in an AX form (before the record is committed to the database). A developer must explicitly call this method when creating/updating records via code.
  • validateDelete: This method validates records to be deleted and checks for delete actions. This method's data is automatically triggered when a user tries to delete the record in an AX form (before the record is physically deleted from the database). A developer must explicitly call this method when deleting records via code.
  • validateField: This method checks whether the value of the given field is a legal value. This method's data is automatically triggered when a user writes or updates data on a field in an AX form (before the field is committed to the database). A developer must explicitly call this method when creating/updating records via code.

Validation rules (set using table/form metadata) are automatically triggered when the user creates, updates, or deletes a record using a form. If, however, the same actions are performed using the X++ code, the developer needs to write the code to validate the action. Another type of validation methods called aosValidate methods are used to ensure that the user performing the action has the proper access rights according to the user group setup.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.10.246