Apex Triggers in Salesforce

 


Apex Triggers in Salesforce

A Trigger is a segment of Apex code which executes before or after inserting or modifying a Salesforce record based on the condition provided. There are different types of triggers based on the action going to be performed.

What is Trigger in Salesforce ?

Trigger in Salesforce is essentially an Apex script used by developers before or after events related to data manipulation language (DML).
Here are some of the key types of operations executed by a trigger:

1. Inserting data into the system.

2. Updating data within the system.

3. Deleting data from the system.

4. Merging data across platforms.

5. Upserting data into the system.

6. Undeleting data from the system.


What are different types of Triggers?

  1. Before triggers are used to perform a task before a record is inserted or updated or deleted. These are used to update or validate record values before they are saved to the database.

  2. After triggers are used if we want to use the information set by the Salesforce system and to make changes in the other records. are used to access field values that are set by the system (such as a record’s Id or LastModifiedDate field), and to affect changes in other records. The records that fire the after the trigger is read-only.


Trigger events in salesforce?

A trigger is a set of statements which can be executed on the following events.
In trigger events, one or more of the below events can be used with comma-separated.
  1. before insert
  2. before update
  3. before delete
  4. after insert
  5. after update
  6. after delete
  7. after undelete


What are context variables in triggers?

  1. isExecuting: Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an execute anonymous() API call.

  2. isInsert: Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. eg. trigger.isInsert

  3. isUpdate: Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.

  4. isDelete: Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.

  5. isBefore: Returns true if this trigger was fired before any record was saved.

  6. isAfter: Returns true if this trigger was fired after all records were saved.

  7. isUndelete: Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)

  8. new: Returns a list of the new versions of the sObject records. This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.

  9. newMap: A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.

  10. old : Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers.

  11. oldMap: A map of IDs to the old versions of the sObject records. This map is only available in update and delete triggers.

  12. size: The total number of records in a trigger invocation, both old and new.


 Trigger Context Variable Considerations:


 

Trigger Best Practices:

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.


Apex Triggers Video Tutorial:


# Resources:

Trailhead : Apex Trigger 



Comments

Popular Posts

Asynchronous Apex in Salesforce : Future Methods

Salesforce Admin Interview Questions and Answers

DML - Data Manipulation Language in Apex