Triggers perform limited recursion - see e.g. Triggers and Order of Execution - so changes will not automatically cascade down many levels. But they can execute more than once, so it is probably best to start any trigger with a class static variable guard to ensure that the modification code only runs once:
if (!GuardClass.isObjectXUpdating) {
try {
GuardClass.isObjectXUpdating = true;
// Recursive algorithm goes here
} finally {
GuardClass.isObjectXUpdating = false;
}
}
Here is a "recursive algorithm" that follows the parent to child relationship down an open ended number of levels and collects all changes into a single update list:
List<ObjectX__c> updates = new List<ObjectX__c>();
Map<Id, ObjectX__c> parents = Trigger.newMap;
do {
Map<Id, ObjectX__c> children = new Map<Id, ObjectX__c>([
select Id, ParentObjectX__c, ...
from ObjectX__c
where ParentObjectX__c in :parents.keySet()
]);
// Add code to modify children map data using data from parents map as required
updates.addAll(children.values());
parents = children;
} while (parents.size() > 0);
updates updates;
The current Execution Governors and Limits allow 100 queries to be made and 10,000 records to be updated. So assuming little else is going on in the transaction, close to 100 parent-child levels can be spanned (i.e. a lot), and close to 10,000 child objects can be updated (more likely to be a problem).
The above is a simple synchronous solution. If you think your data will push you over the 100 or 10,000 governor limit, then an asynchronous approach will be needed such as kicking off a Batchable
. (You can only have up to 10 @future
requests outstanding at once so my thinking is that the sequential nature of Batchable
is generally more predictable.) Probably best to ask a separate question if that is the case, explaining that the volumes of data will be large.
(FYI the situation is a little different when going from child to parent because SOQL allows multiple levels to be queried at once so allowing fewer queries to be used. The tree also naturally narrows going in that direction.)
Best Answer
You can absolutely do this with
Process Builder
. When you get to the point where you are selecting your action chooseUpdate Records
. Then you can selectRecords related to your Account
or whatever custom object you replaceAccount
with. From the dropdown you can select a parent relationship.