+ Start a Discussion
giri rockzzzzgiri rockzzzz 

How to create bulkified trigger???

How to create bulkified trigger???can anyone give me sample example

????

Imran MohammedImran Mohammed

Below are few suggestions,

 

  • Make use of collections like Maps, Sets, Lists
  • Avoid issuing queries inside the for loop.
  • Dont issue DML commands inside for loop and not for each record.
  • Gather all the necessary data to be used before you issue a SOQL query.
Ex:
Lets say you have trigger on Opportunity which will insert records in another object when Stage reaches Closed Won
trigger opp on Opprtunity(after update)
{
List<Object__c> newRecordList = new List<Object__c>();
for(Opportunity o: Trigger.new)
{
if(o.StageName = = 'Closed Won')
{
Object__c ob = new Object__c(name = o.name, other fields);
newRecordList.add(ob);
}
}
if(newRecordList.size() > 0)
{
insert newRecordList;
}
}
Above example will be good for you to start up with.
There could be many other scenarios which i did not cover.

 

Pradeep_NavatarPradeep_Navatar

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch. For example, a trigger could be invoked by an Force.com Web Services API call that inserted a batch of records. So if a batch of records invokes the the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.

 

Here is an example of poorly written code that only handles one record:

 

    trigger accountTestTrggr on Account (before insert, before update) {
 
       //This only handles the first record in the Trigger.new collection
       //But if more than 1 Account initiated this trigger, those additional records
       //will not be processed
       Account acct = Trigger.new[0];
       List<Contact> contacts = [select id, salutation, firstname, lastname, email
                  from Contact where accountId = :acct.Id];
       
    }

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Here is a sample of how to handle all incoming records:

    trigger accountTestTrggr on Account (before insert, before update) {
     
       List<String> accountNames = new List<String>{};
     
       //Loop through all records in the Trigger.new collection
       for(Account a: Trigger.new){
          //Concatenate the Name and billingState into the Description field
          a.Description = a.Name + ':' + a.BillingState
       }
       
    }

 

For more examples, go through the URL http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/

 

Hope this helps.

 

 

Here is an example of poorly written code that only handles one record:

01trigger accountTestTrggr on Account (before insert, before update) {
02 
03   //This only handles the first record in the Trigger.new collection
04   //But if more than 1 Account initiated this trigger, those additional records
05   //will not be processed
06   Account acct = Trigger.new[0];
07   List<Contact> contacts = [select id, salutation, firstname, lastname, email
08              from Contact where accountId = :acct.Id];
09    
10}

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Here is a sample of how to handle all incoming records:

01trigger accountTestTrggr on Account (before insert, before update) {
02 
03   List<String> accountNames = new List<String>{};
04  
05   //Loop through all records in the Trigger.new collection
06   for(Account a: Trigger.new){
07      //Concatenate the Name and billingState into the Description field
08      a.Description = a.Name + ':' + a.BillingState
09   }
10    
11}