Referential integrity – delete actions and validation

It is critical that we enforce data integrity on the tables we create. AX assists us a lot with this by creating relations. AX will use the relations we created earlier to ensure that data entered through the user interface is valid. Simply put, we can only enter a vehicle group on the vehicle table that exists in the vehicle group table. It is more accurate in this example to say that the data entered in the vehicle group field is validated against the relation; the relation might have an additional constraint.

AX handles that data entry validation, but what happens when we want to delete a vehicle group? The system cannot know the answer to this because we have different scenarios here, as shown in the following table:

Foreign table

Related table

Action

ConFMSVehicleGroup

ConFMSVehicleTable

Restrict deletion

ConFMSVehicleSchedule

ConFMSVehicleTable

Restrict deletion

ConFMSVehicleTable

ConFMSVehServiceTable

Cascade

This is what delete actions are used for—to define this behavior. We configure this in the foreign table as per the following steps:

  1. On the DeleteActions node for the ConFMSVehicleGroup table, right-click and select New DeleteAction.
  2. Type ConFMSVehicleTable in the Table property (paying attention to autocompletion).

    Note

    Relation is set automatically to ConFMSVehicleGroup, which is great. For tables with multiple relations to this table, we can specify the correct relation using this property.

  3. Set DeleteAction to Restricted.
  4. Do the same for ConFMSVehicleSchedule > ConFMSVehicleTable.
  5. Create the delete action for ConFMSVehicleTable > ConFMSVehServiceTable, but make DeleteAction property Cascade.

How this works

The delete action has the following options, None, Restricted, Cascade, Cascade + Restricted:

  • None: This has no effect, and effectively disables the delete action. This is useful if you want to specifically state "do nothing" so that someone else doesn't try to correct what seems to be an omission.
  • Restricted: This will prevent the record from being deleted if there are records in the related table that match the selected relation. This occurs within the validateDelete table event, which is called by the delete table event.
  • Cascade: This will delete the record in the related table, based on the relation. A sales order line is of no use without a sales order. This is an extension of the delete table event.
  • Cascade + Restricted: This is a little special. In a two-table scenario, it is the same as Restricted. It will stop the record from being deleted if a related record exists. But if the record is being deleted as part of a cascade from a table related to it, the records will be deleted.

    Note

    See How to: Create Delete Actions at http://msdn.microsoft.com/en-us/library/bb315018.aspx.

For example, in the following scenario we have invented a grandparent table above ConFMSVehicleTable, called ConFMSHaulierTable:

How this works

If we try to delete a record from ConFMSVehicleTable and there are related records in ConFMSVehServiceTable, the deletion will not be allowed.

If we delete a record from ConFMSHaulierTable, it will also delete related records in ConFMSVehicleTable and cascade the deletion so that the related ConFMSVehServiceTable records will be deleted as well.

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

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