Features of Apex Triggers

  1. Triggers are executed in real-time, as soon as a user performs an action that affects records.
  2. Triggers can be used to modify data before or after it is saved to the database.
  3. Triggers can be used to enforce data validation rules and ensure data integrity.
  4. Triggers can be used to automate business processes and perform custom actions.
  5. Triggers can be used in combination with other Salesforce features, such as workflows and process builder, to create complex business automation workflows.

When to use Trigger and When to use Flow in Salesforce?

In Salesforce, both triggers and flows can be used to automate business processes and perform custom actions. However, they are used in different situations and have different strengths and limitations.

Apex triggers are typically used when more complex logic is required or when data needs to be manipulated at a low level. Triggers are written in Apex code and can execute both before and after data is saved to the database. This allows them to manipulate data at a very low level, perform calculations and validations, and interact with external systems. Triggers are very powerful and flexible, but can be more difficult to write and maintain than other automation tools in Salesforce.

On the other hand, flows are used when a more visual, user-friendly approach is required. Flows are created using a point-and-click interface and can be used to automate business processes by guiding users through a series of screens and actions. Flows are designed to be easy to use and require minimal coding, making them ideal for less complex business processes. Flows also provide a higher degree of control and visibility over the automation process, and can be used to build complex workflows with branching logic, error handling, and other advanced features.

In general, triggers are best suited for complex business processes that require a high degree of control and low-level data manipulation, while flows are best suited for simpler processes that require a user-friendly interface and easy configuration. Ultimately, the choice between triggers and flows will depend on the specific needs of your business and the complexity of the processes that you are trying to automate.

Trigger syntax 

  • TriggerName is a name you choose to give your trigger. It should be descriptive of what the trigger does.
  • ObjectName is the name of the object for which you are creating the trigger (e.g., Account, Contact, Opportunity).
  • trigger_events are the events that will trigger the code to execute (e.g., before insert, after update).
  • The opening brace { marks the beginning of the trigger code.
  • The trigger code is written in Apex, Salesforce’s programming language.

Trigger Events in salesforce

before insert: Triggered before new records are inserted into the database.

before update: Triggered before existing records are updated in the database.

before delete: Triggered before existing records are deleted from the database.

after insert: Triggered after new records are inserted into the database.

after update: Triggered after existing records are updated in the database.

after delete: Triggered after existing records are deleted from the database.

after undelete: Triggered after deleted records are recovered from the recycle bin.

Before Trigger with example

After Trigger with example

Best practices in apex trigger 

1. Use bulkified code: Apex triggers should be written to handle bulk operations, not just single records. This means that you should use collections to process multiple records at once, rather than querying the database for each individual record. 

2.Check governor limits: Apex triggers are subject to governor limits, which restrict the amount of resources that a trigger can use. Make sure to write efficient code and use bulk operations to avoid hitting these limits.

3.Test thoroughly: Always test your triggers thoroughly to make sure that they are working as expected. Write unit tests to cover all possible scenarios, including bulk operations and error conditions.

Important considerations to keep in mind when using context variables:

1.Context variables are only available during the trigger’s execution: Context variables such as Trigger.new, Trigger.old, and Trigger.newMap are only available during the execution of the trigger. They cannot be accessed outside of the trigger, so if you need to store data for use in another context, you will need to copy the data to another variable.

2. Context variables are read-only: The data contained in context variables is read-only, which means that you cannot modify it directly. If you need to update or delete records, you will need to copy the data to another variable and make the changes there.

3.Context variables are collections: Context variables such as Trigger.new and Trigger.old are collections of records. This means that you can use collection methods such as size(), isEmpty(), and contains() to work with the records in the collection.

4.Context variables are subject to governor limits: Apex triggers are subject to governor limits, which restrict the amount of resources that a trigger can use. Make sure to write efficient code and use bulk operations to avoid hitting these limits.

Recursive Trigger and How to avoid it?

Recursive triggers occur when a trigger on an object triggers another trigger on the same object, resulting in an infinite loop of trigger execution. This can lead to performance issues and can even cause the system to crash.

To avoid recursive triggers in Salesforce, you can use a static variable to keep track of whether the trigger has already run. By using a static variable to keep track of whether the trigger has already run, we can avoid recursive trigger execution and prevent performance issues in our Salesforce org.

Bulkifying trigger in salesforce 

Bulkifying a trigger means writing the trigger logic in a way that it can handle multiple records at once, rather than processing each record individually. This helps to reduce the number of database operations and improve performance.

Here’s an example of how to bulkify a trigger in Salesforce:

In this example, we are using the Trigger.new context variable to access the new and updated accounts, and adding their IDs to a set. We then use the set to query the related contacts for the accounts using a single SOQL query, rather than querying the database for each individual record. We store the related contacts in a map, with the account ID as the key and a list of contacts as the value.

We then loop through the new and updated accounts again, and set a custom field based on the number of related contacts stored in the map. By using a single query and storing the related contacts in a map, we can handle multiple records at once and avoid hitting governor limits.

Similar Posts