I want to build an update trigger on a rollup summary field.
Say I have a parent record A, A have 1000 children records linked with a Master-Detail relationship.
1.First Operation: delete 1000 records, if the code is like
List<Child__c> children = [select id from Child__c where parentid = A.Id];
delete children;
How many times would the update trigger on parent record A run?
Is it 5? What if the second 200 batch is being deleted when the first update trigger is running? I'm confused with the execution order and the events start&end order.
2.Second Operation: insert 1000 child records from data loader or any external app.
How many times would the update trigger on parent record A run?
Is it 5? Is it similar to the delete operation?
Thanks
Update
The Roll up summary field is a "SUM" of it's children records field num__c which has a number type.
The question assumes the num__c field of the deleted&inserted children records have value on this field.
The question also specified 1000 child records belongs to a same parent record
Best Answer
The update triggers on parent will be fire .200 child records are processed at a time when you delete 1000 records .This means there will be 5 chunks .Lets say 200 child belongs to 10 parent records ,trigger.new will have 10 parent records .
The trigger on Parent would run for update case .In your case you are deleting or inserting and that should trigger update triggers on parent records .
To confirm this behavior just did a quick experiment
1.Create a parent object and a child object .Associate them via master detail .
2.Create a number field on the child record .
3.Create a roll up summary field on the parent record summing the number field that's created in previous field
4.Create a trigger on the parent record like below
5.Delete or insert the data into child objects via data loader or developer console.You should see the add error indicating triggers fire