Posts

Showing posts from September, 2017

Best Practices for Apex code

1. Bulkify the code       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 a 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 bulk, in order to write scalable code and avoid hitting governor limits.      Here is an example of written code that only handles one record: trigger accountTrigger 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 = [ s