Activity › Forums › Salesforce® Discussions › How to bulkify a code in Salesforce Apex?
Tagged: Bulkify Code, Coding in Salesforce, Salesforce Apex, Salesforce Apex Class, Salesforce Apex Code, Salesforce Apex Function, Salesforce Code
-
How to bulkify a code in Salesforce Apex?
Posted by chanchal kumar on June 28, 2018 at 1:38 PMHow to bulkify a code in Salesforce Apex?
shradha jain replied 7 years, 8 months ago 4 Members · 3 Replies -
3 Replies
-
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];}
- [adinserter block='9']
-
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.
-
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
Log In to reply.