APEX TRIGGER 1. Basics: - Apex Triggers are special blocks of code that automatically execute when specific events occur in Salesforce, such as creating, updating, or deleting records. - They are used to implement custom business logic and automation in response to these events. 2. Definition: - Apex Triggers are snippets of Apex code that are associated with a specific object, and they are executed before or after data manipulation events (like insert, update, delete, or undelete) occur on that object's records. 3. Types of Triggers: - There are two main types of triggers: "Before" triggers and "After" triggers. - "Before" triggers are executed before the record is saved, allowing you to validate and potentially modify the data. - "After" triggers are executed after the record is saved, and they are often used to perform additional tasks like sending emails or creating related records. 4. Trigger Order of Execution: - In Salesforce, triggers are executed in a specific order: 1. Before Triggers: Validation rules, duplicate rules, and "Before" triggers. 2. System Validation: Salesforce performs standard system validation (field updates, flows, and processes). 3. After Triggers: "After" triggers, workflow rules, and processes. 4. Assignment Rules: If the record is assigned to a user or queue. 5. Auto-Response Rules: If email-to-case or web-to-case is configured. 6. Post-System Logic: Any additional processes, such as sending email alerts. 5. Trigger Context Variables: - When a trigger is executed, it has access to trigger context variables like `Trigger.new`, `Trigger.old`, and `Trigger.newMap`. `Trigger.new`- It is a list which contains Records with new values `Trigger.newMap`- It is a Map contains <ID,record> of new values https://www.linkedin.com/in/muralikrishna-s/ APEX TRIGGER `Trigger.old`- It is a list which contains Records with old values `Trigger.oldMap`- It is a Map contains <ID,record> of old values 6. System Variables: Apex Triggers in Salesforce have several system variables that provide information about the context in which the trigger is executed. These system variables are essential for understanding and controlling the behavior of your trigger. Here are some commonly used system variables in Apex Triggers: 1. `Trigger.isExecuting`: This Boolean variable indicates whether the current code is executing within a trigger context. It is typically used to check if code is being run as part of a trigger or not. 2. `Trigger.isBefore`: A Boolean variable that is `true` if the trigger is executing in the "before" context (before records are saved), and `false` in the "after" context. 3. `Trigger.isAfter`: Similar to `Trigger.isBefore`, this variable is `true` if the trigger is executing in the "after" context (after records are saved), and `false` in the "before" context. 4. `Trigger.isInsert`, `Trigger.isUpdate`, `Trigger.isDelete`, and `Trigger.isUndelete`: These Boolean variables indicate the specific DML (Data Manipulation Language) operation that fired the trigger. For example, `Trigger.isInsert` is `true` in an insert trigger and `false` otherwise. 5. `Trigger.newSize` and `Trigger.oldSize`: These variables provide the number of records in the `Trigger.new` and `Trigger.old` lists, respectively. They are often used to check how many records are involved in the trigger operation. https://www.linkedin.com/in/muralikrishna-s/ APEX TRIGGER 6. `Trigger.operationType`: This variable provides the DML operation type as an enumeration. For example, it can be used to determine whether the trigger is handling an INSERT, UPDATE, DELETE, or MERGE operation. 7. `Trigger.contextUser`: This variable returns the user who initiated the trigger's execution. It can be used to identify the user responsible for the data change. These system variables help Apex Triggers react to specific scenarios and make decisions based on the context and data changes in your Salesforce organization. 6. Best Practices for Triggers: - Some best practices for writing triggers include: - Keeping triggers simple and focused on a single task. - Bulkifying your code to handle multiple records in a single trigger execution. - Using helper classes to organize code and make it more maintainable. - Writing unit tests to ensure your triggers work correctly and to meet Salesforce deployment requirements. 7. Trigger Exceptions: - Triggers can throw exceptions, and it's important to handle them properly. For example, you might catch exceptions and handle them by sending error messages to users or logs. - Exception handling ensures that your triggers don't crash and disrupt the overall functionality of your Salesforce system. These points should give you a comprehensive understanding of Apex Triggers in Salesforce, from their basic concept to best practices and potential exceptions. https://www.linkedin.com/in/muralikrishna-s/ APEX TRIGGER 8. Syntax: The syntax for creating an Apex Trigger in Salesforce follows a specific structure. Here is the basic outline for an Apex Trigger: Syntax: trigger TriggerName on ObjectName (trigger_events) { // Trigger code here // It can be "Before Insert", "Before Update", "After Insert", "After Update", etc. // You can specify multiple trigger events using commas. // For example, "Before Insert, After Update" if (Trigger.isBefore) { if (Trigger.isInsert) { // Code to be executed before records are inserted } else if (Trigger.isUpdate) { // Code to be executed before records are updated } // Other conditions for "Before" trigger events // You can use Trigger.new and Trigger.old to access the records. } else if (Trigger.isAfter) { if (Trigger.isInsert) { // Code to be executed after records are inserted } else if (Trigger.isUpdate) { // Code to be executed after records are updated } // Other conditions for "After" trigger events // You can use Trigger.new and Trigger.old to access the records. } https://www.linkedin.com/in/muralikrishna-s/ APEX TRIGGER // Additional trigger code, if needed } https://www.linkedin.com/in/muralikrishna-s/