Activity Forums Salesforce® Discussions How can trigger is used for bulkifying records in Salesforce?

  • Mohit

    Member
    August 30, 2016 at 8:40 am

    Hi Pranav,

    Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

    Originally ,  Trigger or method that ordinarily only receives a single record as input, will receive a hundred - to run correctly in this context (and others) it should be written to handle multiple records.When a batch of records initiate Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch.  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 the Example:-

    trigger accountTestTrggr on Account (before insert, before update) {

    List<String> accountNames = new List<String>{};
    for(Account a: Trigger.new){
    a.Description = a.Name + ':' + a.BillingState
    }
    }

    If this trigger is invoked with a single Account or up to 200 Accounts, all records will be properly processed.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos