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
kevinjia1984kevinjia1984 

Question About Bulk Operation Of Data

Hi all,

 

Just want to ask if I add a for loop just inside the trigger bracket will the trigger has the ability to handle bulk operation of Data? ie.

 

trigger ContactRelationshipTrigger on Contact (before insert, before update) {

 

for(Contact CON : Trigger.New){

...

}

}

 

If not, how to realize it? Thanks. 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Yes, going around a for loop is the beginning of bulk-ificiation

 

It needs to be coupled however with things like consolidating SOQL queries over aggregated criteria data, rather than a soql query for every record in your for loop. Ditto for updates.

 

eg

 

 List <Id> accIds = new List<Id>{};

for(Contact con : Trigger.New){

accIds.add(con.AccountId);   // aggregate account ids in a list, to query for all accounts at once in one soql query later

}

 

Accounts[] accs = [Select Id, Name from Account where Id IN:accIds];