Delete Operations in DDOs
A DDO delete is performed by sending Request_Delete to a DDO. In some cases, you will use objects such as data-entry objects (DEOs) to handle the entire delete for you. In other cases, you will need to write custom code to perform the delete. In all cases, the delete process itself is the same.
Request_Delete Message
The Request_Delete message is responsible for deleting a record in the DDO for the main table, updating all parent DDOs (and tables), and possibly deleting all child records.
Two modes of deleting are supported:
- A delete will delete all related descendant records.
- Deletes will not be allowed when child records exist.
The property Cascade_Delete_State controls this message.
Cascade_Delete_State is True
If Cascade_Delete_State is true, Request_Delete will attempt to delete the DDO’s record and all descendant records as follows:
- Verify that the DDO structure is valid for the cascade delete operation.
- Begin Transaction – From this point on, tables are locked (if the database uses table locking) or records are locked as they are found (if the database uses record locking).
- Re-Find all required records.
- Get
Validate_Deleteof the main DDO. - If validation fails, cancel operation.
- Delete all related descendant records. For each child record:
- Send
Deletingto the child DDO. - Send
Backoutto all parent DDOs. - Attach and Save all parent tables (Attach_Main_File, Save_Main_File).
- Delete the child record (Delete_Main_File).
- Delete the record of the main DDO.
- Send
Deletingto the main DDO. - Send
Backoutto all parent DDOs. - Attach and Save all parent tables (Attach_Main_File, Save_Main_File).
- Delete the main record (Delete_Main_File).
- End Transaction – Either commit changes or roll back changes. Unlock tables.
- If delete was successful, send Refresh to all participating DEOs.
Cascade_Delete_State is False
If Cascade_Delete_State is false, Request_Delete will not allow users to delete a record when a child-file record(s) exists as follows:
- Verify that the DDO structure is valid for the no-cascade delete operation.
- Begin Transaction – From this point on, tables are locked (if the database uses table locking) or records are locked as they are found (if the database uses record locking).
- Re-Find all required records.
- Get
Validate_Deleteof the main DDO. - If validation fails, cancel operation.
- Check for any child records or closed tables (get Validate_Delete_No_Cascade).
- If validation fails, cancel operation.
- Send
Deletingto the main DDO. - Send
Backoutto all parent DDOs. - Attach and Save all parent tables (Attach_Main_File, Save_Main_File).
- Delete the main record (Delete_Main_File).
- End Transaction – Either commit changes or roll back changes. Unlock tables.
- If delete was successful, send Refresh to all participating DEOs.
Handling Errors
If an error occurs anywhere within a delete, the transaction is rolled back, the database is unlocked, and the error is reported.
Most often, the error will occur within the Validate_Delete event and will be a “controlled” error (i.e., an error purposefully created by the programmer). This must be a real DataFlex error generated using the UserError message or the Error command. The generation of an error is what triggers a transaction rollback. The reporting of the error will be properly deferred until the transaction is rolled back and your database is unlocked.
Although Validate_Delete is the event specifically created to allow you to perform one last validation, you may stop a transaction within any of the delete events (Deleting, Backout, Delete_Main_File, Attach_Main_File, Save_Main_File). If you generate an error, the transaction will be rolled back.