What are the best practices for trigger in Salesforce?

What are the best practices for trigger in Salesforce?

Apex Trigger Best Practices

  • One Trigger Per Object. We should write only 1 Trigger per Object.
  • Follow Naming Convention.
  • Use only Required Events in Trigger.
  • Context-specific Handler Methods.
  • Logic-less Triggers.
  • Bulkification.
  • Use Collections and Avoid SOQL or DML in FOR loops.
  • Use SOQL Query FOR Loop.

When do you need to bulkify trigger in Salesforce?

If you do that, your trigger is guaranteed to hit the governor limits. If you need to fetch large amount of data from an object (More than 50,000 records), then go for batch apex to pull that data, else you will again bump into another governor limit that wouldn’t allow you to fetch more than 50,000 records.

Which is an example of how to bulkify?

The good news is that 90% of people I interview do not understand how to bulkify, so this is your chance to stand out! Bulkification Example: Let’s analyze the governor limit for DML statements. This code is not bulkified and will go over the 150 DML statements limit: insert t; // You’ll get an error after the 150th opp!

What does it mean to bulkify Apex code?

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. 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

How many records can be entered in trigger in Salesforce?

Up to 200 records can enter your trigger at once! Remember Trigger.new? Salesforce will batch up mass updates and include up to 200 records at once in it (this commonly happens when using tools like Data Loader). So for example, if you do one SOQL query per record, you’re going to go over the 100 SOQL query limit!