• #29494
    Forcetalks

    How to bulkify a code in Salesforce Apex?

    How to bulkify a code in Salesforce Apex?

     

    #31327
    Forcetalks

    Hi Chanchal,

    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.

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

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

    Account acct = Trigger.new[0];
    List<Contact> contacts = [select id, salutation, firstname, lastname, email
    from Contact where accountId = :acct.Id];

    }

    #33306
    Salesforce | Parul Parul #2
    Forcetalks

    Hi Chanchal,

    The benefit of bulkifying your code is that bulkified code can process large numbers of records efficiently and run within governor limits on the Lightning Platform. These governor limits are in place to ensure that runaway code doesn’t monopolize resources on the multitenant platform.

    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.

    Apex triggers are optimized to operate in bulk. We recommend using bulk design patterns for processing records in triggers.

    For example:

    trigger InsertContact on Contact(before insert) {

    List<Contact> contactsList=new List<Contact>();

    for(Contact conlist: Trigger.new){

    conlist.name=’Parul’;

    contactsList.add(conlist);

    }

    insert contactsList;

    }

     

    Thanks.

    #34726
    Forcetalks

    Hi,

    To bulkify your code means to combine repetitive tasks in Apex! It’s the only way to get around Governor Limits .

    For Example:

    This code is not bulkified and will go over the 150 DML statements limit:

    // Remember that up to 200 records can be in Trigger.new
    for (Opportunity opp : Trigger.new) {
    Task t = new Task();
    t.Name = ‘Give your prospect a free t-shirt’;
    t.WhatId = opp.Id;
    insert t; // You’ll get an error after the 150th opp!
    }

    This code is bulkified and will not hit any governor limits. It uses Lists and will only do one DML statement no matter how many records are in the trigger:

    // Do an insert DML on all tasks at once using a List
    List<Task> taskList = new List<Task>();
    for (Opportunity opp : Trigger.new) {
    Task t = new Task();
    t.Name = ‘Give your prospect a free t-shirt’;
    t.WhatId = opp.Id;
    taskList.add(t);
    }
    insert taskList; // Notice this is outside the loop

Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?