Bulk Enabled Triggers in Salesforce:

it is strongly recommended that you make it bulk enabled, so that the trigger can handle number of records whcih can be inserted/updated/deleted through data loader or any other data migration tool.

Here is the Bulk trigger Example:

Trigger myTrigger on Account(after update)
{
   
    Set <Id> setAccId = new Set<Id>();

    for(Account a: Trigger.new)
    {
        if(a.isActive)
            setAccId.add(a.Id);//set always contains distinct Ids
    }
   
    list <Opportunity> lstOpp = [select Id, StageName from Opportunity where AccountId in : setAccId];
    for(Integer i = 0; i < lstOpp.size(); i++) {
        lstOpp[i].StageName = "Learning";
    }
    if(lstOpp.size() > 0)
        update lstOpp;

}

Written by 

2x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*