Activity Forums Salesforce® Discussions What is meant by bulkifying trigger?

  • Parul

    Member
    August 29, 2018 at 12:56 pm

    Hello Anurag,

     

    Bulkifying triggers means making sure the trigger can operate when multiple (hundreds) of records are passed in at once.

    When a trigger fires, you get your "list" of objects in Trigger.new (and some other lists/maps).

    Typically when you imagine a trigger firing, you do so thinking that a user has just affected one record, and so this list has one item in it. but in reality, Salesforce could send up to 200 records into the trigger in one list. If you are data loading, or mass updating (or many other things) - this can result in all the items being sent to the trigger in one go.

    So, if you had a trigger, that took the Trigger.new list, and did a for() loop through the items and executed a SOQL query to load some data for each record, because there are govenor limits (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm) in Salesforce - if the trigger.new list had more than 100 items in it, the trigger would completely fail - because you made too many SOQL querys, and your data update/insert would be rolled back.

    Making sure these problems don't exist is bulkifying triggers - ie. making sure they can work with multiple (bulk loads) of data.

    There is literally tonnes of blog posts and developer articles on this topic all over the internet:
    https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=salesforce+bulkify+trigger (https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=salesforce+bulkify+trigger)

    Including an official Salesforce post here:
    https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

     

    Thanks

    Parul

  • shariq

    Member
    September 14, 2018 at 10:30 pm

    Hi,

    To get it more simple bulkifying means that it should work for multiple records.

    The term 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 initiate 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. Or a custom Apex Web Service. 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.

    for example :-

    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
    }

    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos