When building your application it is important to consider how deleting a parent record will affect any child or linked records. If you delete a parent record with other records linked to it you are removing the link on the child record and effectively abandoning it with no parent. These parent-less child records take up unnecessary space in your database and can cause problems. Best practice is to delete them at the same time you delete the parent record.
Deleting Child Records Overview
There are several ways you can delete child records. The easiest way is to create a Background view that filters the child records by the linking property and then performing the delete action.
Here are the basic steps to deleting child records:
- On the parent view Delete action add a store variable action to store the Record Code property of the parent record to a variable.
- Create a Background view from the child Record Type that filters on the variable stored in step 1.
- On the Background view create an On Open action that will have the following actions on it.
- Add the Set Selection action and configure it to select all recordson the view.
- Add the Delete action configured to 'Do Not Prompt'
- Lastly add the Close action, to close the background view once it has deleted all the records.
- Add the newly created OnOpen action to the OnOpen event on the background view.
- On the parent view Delete action after the store variable action add the Open View action to open you Background view as hidden. As soon as the background view is opened it will execute the OnOpen action you created in step 3 deleting the child records.
- Add the Delete action after the Open View action to finish by deleting the parent record.
Tips, Tricks, and Examples
In this example we are going to delete Account record and all its child records (Incomes, Expences).
We are going to create "Delete" action on the Parent view.
"Delete" button performs the actions:
- Check to see if ID (record code property) is null or empty, to make sure record is selected.
- Show message to confirm the user wants to delete a record. If the user selects 'No' the delete action is cancelled.
- Set view as busy.
- Call deleteChildRecords() action. (see below for more information on the deleteChildRecords action)
- Delete parent record.
- Turn the busy indicator off.
The Busy indicator does 2 things:
- It indicates to the user that the deletion process is occuring and
- It ensures the user does not do anything to the view during deletion process.
If you are closing the ciew at the end of the action chain you do not need to turn the busy indicator off.
The deleteChildRecords() action chain performs the following actions:
- Store parent Account ID (Record Code property) to a variable.
- Opens background view to delete child records.
- Call deleteAllRecordsAndClose() action from background view
The '_background_DeleteChildRecordsView' background view is configured to only display records linked to our main record. We are using the stored variable for Account ID (Record Code property) that is created in step 1 above.
The 'deleteAllRecordsAndClose()' action is located on the background view and performs the following actions:
- Select all records.
- Delete selected records without prompt.
- Close the background view.
Once this configuration is set, every time the delete action on the parent view is executed the parent record as well as any associated child records will be deleted.