Best way to delete related subtable records when a record is deleted on subtable main table?

I’m working with a setup where I have a main table that includes a subtable main. I want to allow users to delete a record from the subtable in the UI.

Here’s the challenge:
When a user deletes a subtable record and clicks Save, I also want to automatically delete any other subtable records that are related to the one the user deleted as well as delete some related records on a different table.

What’s the best way to implement this?

My gut says to use a Trigger on the subtable main. When it registers a delete, it fetches the parent record and deletes all related subtable records from it before going and deleting other related records.

You should be careful to use a Post-Delete trigger, so that the first subtable record is already deleted when the triggers for the other subtable records run, otherwise you’ll find yourself processing that first record again.

It would be nice if you could use an action block on the subtable main instead, so that you don’t have the reentrant trigger problem at all, but I believe there are other issues with subtable mains and action blocks which prevent that from working.

Alternatively, you could consider an update trigger on the parent which does a Fetch Original Record to compare the two subtables, see what changed, and process accordingly there. That’s more overhead but will guarantee that the logic only happens once per transaction once it’s implemented.

@cheyanne.schwiethale Can you please confirm whether action blocks + subtable mains have problems? Do you have a better idea for how Bethany can solve her problem?

@brendan.b you are correct in that the platform does not currently support Action Blocks on a Subtable Main.

However, the way that we recommend to delete additional subtable records is not through a trigger on the subtable itself, but rather in a trigger on the parent record. Then you can use a combination of the Loop Subtable and Delete Subtable Record actions.

1 Like