function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Galeeb SKGaleeb SK 

Bulkifying the Trigger

Hi

what is Bulkifying Trigger in  salesforce?

According to my knowledge Bulkifying Trigger means to combine two or more triggers into one trigger ,is it right (or) wrong tell me the resonable Answer

Regards
Galeeb SK
srlawr uksrlawr uk
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



 
Amit Chaudhary 8Amit Chaudhary 8
No Bulkify Trigger dnt mean two trigger into one trigger. Please check below post. I hope that will help you
1) http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html
2) https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
3) https://developer.salesforce.com/page/Apex_Code_Best_Practices

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 SOAP API call that inserted a batch of records. So if a batch of records invokes 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 one 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.newcollection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in theTrigger.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
   }
   
}



1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.

3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers

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

5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately
I
t is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

Please let us know if this will help u

Thanks
Amit Chaudhary
Galeeb SKGaleeb SK
Thank you  srlawr uk,Amit Chaudhary for your Answers

Regards
Galeeb SK