function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shakena WatsonShakena Watson 

Apex Trigger to Process Bulk records

I will be calling an Apex process to update an object field (not a formula field) when there is an insert, 
update or delete. 

It will update the object field for all records in an object meeting an criteria, not just the records in the 
trigger.new, trigger.old, etc., according to criteria.

Is a trigger the best place to call the process, since it is during a bulk up. 

Should I be using something else (Flow, etc.).

I assume a trigger is the only way know if an object record is inserted into, updated, or deleted .

If a trigger is the route I need to take I need an example of how to call the process in an Apex trigger.

Thanks.
VinayVinay (Salesforce Developers) 
Hi Shakena,

You can try using trigger and the advantage of bulkifying  apex trigger is it can handle a large number of records efficiently.

Check below references that can help you.

https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_bulk#:~:text=Apex%20triggers%20are%20optimized%20to,likely%20to%20exceed%20platform%20limits.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm
https://www.sfdc-lightning.com/2019/10/bulk-apex-triggers-in-salesforce.html

Thanks,
Shakena WatsonShakena Watson
Hi Vinay,
How is this a bulk trigger different then a regular trigger? In my company we are only allowed to use one trigger per object. Would this require me to create another trigger? I also noticed that the bulk trigger is using the trigger.new in the for loop (for a : trigger.new). As I mentioned in my email the calculation, on an object field, is not only performed on the records in the trigger.new, but all records according to a criteria.
Thanks.
VinayVinay (Salesforce Developers) 
Hi Shakena,

Yes, it is best practice to have one trigger per object and you would need to create a new method in your handler class which will be invoked from trigger.

Something like below.
//Trigger
trigger OpportunityTrigger on Opportunity (after insert, after update, after delete, after undelete) {
new OpportunityTriggerHandler().run();
}

// The Handler Class
public class OpportunityTriggerHandler extends TriggerHandler {

public OpportunityTriggerHandler() {}

}

You might have a separate class to handle trigger variables,  Kindly check your org trigger framework.

https://developer.salesforce.com/blogs/developer-relations/2011/04/apex-trigger-tip-using-a-class-per-object-to-control-logic.html
https://www.sfdcpanther.com/trigger-architecture-framework-recipe-sfdcpanther/

Thanks,