Automatically Deleting Child Records

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:

  1. On the parent view Delete action add a store variable action to store the Record Code property of the parent record to a variable.
  2. Create a Background view from the child Record Type that filters on the variable stored in step 1.  
  3. On the Background view create an On Open action that will have the following actions on it.
    1. Add the Set Selection action and configure it to select all recordson the view.
    2. Add the Delete action configured to 'Do Not Prompt'
    3. Lastly add the Close action, to close the background view once it has deleted all the records.  
  4. Add the newly created OnOpen action to the OnOpen event on the background view.
  5. 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.
  6. 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:

  1. Check to see if ID (record code property) is null or empty, to make sure record is selected.
  2. Show message to confirm the user wants to delete a record. If the user selects 'No' the delete action is cancelled.
  3. Set view as busy.
  4. Call deleteChildRecords() action. (see below for more information on the deleteChildRecords action)
  5. Delete parent record.
  6. 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:

  1. Store parent Account ID (Record Code property) to a variable.
  2. Opens background view to delete child records.
  3. 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.  

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.