You need to sign in to do that
Don't have an account?
Galeeb 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
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
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
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 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
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
It 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
Regards
Galeeb SK