Posts

Showing posts with the label Trigger

Trigger Logic with Apex Switch

Switch Statements Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly. Switch Statements Rethink Trigger Logic with Apex Switch Trigger Context Variables The context variable Trigger.operationType is used to get the current System.TriggerOperation enum value. The following are the values of the System.TriggerOperation enum: AFTER_DELETE AFTER_INSERT AFTER_UNDELETE AFTER_UPDATE BEFORE_DELETE BEFORE_INSERT BEFORE_UPDATE trigger customerTrigger on Account (before insert, after insert) { switch on Trigger.operationType { when BEFORE_INSERT { System.debug('Before Insert'); } when AFTER_INSERT { System.debug('After Insert'); } when else { System.debug('Something went wrong'); } } }

"One Trigger Per Object" Pattern

The " One Trigger per Object " pattern is a best practice in Salesforce development that involves consolidating multiple trigger logic into a single, well-structured trigger handler class per object. This pattern helps to maintain clean and organized code, reduces the risk of trigger order issues, and facilitates better code reuse and maintainability. Trigger Handler Class public class AccountTriggerHandler {          public static void onBeforeInsert(List<Account> newList) {         // Your before insert logic here     }          public static void onBeforeUpdate(List<Account> newList, Map<Id, Account> oldMap) {         // Your before update logic here     }          public static void onBeforeDelete(List<Account> oldList) {         // Your before delete logic here     }        ...

Apex Triggers in Salesforce

Image
In Salesforce, a trigger is a piece of Apex code that executes before or after specific data manipulation events occur, such as inserting, updating, deleting, or undeleting records. Triggers enable you to perform custom actions or validations when these events take place on Salesforce records. Refer to the below links for more details - Get Started with Apex Triggers Apex Triggers Define Apex Triggers Here are some key points about triggers in Salesforce: Execution Context: Triggers execute in response to specific database events and run in the Salesforce environment. Supported Events : Triggers can be defined to execute before or after specific events, including insert, update, delete, undelete, and upsert. Object-Specific: Triggers are associated with specific Salesforce objects, such as Accounts, Contacts, Opportunities, etc. Each trigger operates on records of its associated object type. Apex Language: Triggers are written in Apex, Salesforce's proprietary programming langua...

Not able to create Trigger on Attachment from Salesforce UI

Image
Salesforce doesn't have the option to allow developers to create triggers directly on standard objects like Attachment using the Salesforce UI.  Instead, we've used tools  Force.com IDE in Eclipse to write and deploy triggers on Attachment. For the Attachment, ContentDocument, and Note standard objects, you can’t create a trigger in the Salesforce user interface. For these objects, create a trigger using development tools, such as the Developer Console or the Salesforce extensions for Visual Studio Code. Alternatively, you can also use the Metadata API. Here's an example

How to Avoid recursive trigger in salesforce ?

Avoiding recursive triggers in Salesforce is crucial to prevent infinite loops and excessive consumption of system resources. Here are several strategies to achieve this: Use a Static Variable or Flag : Utilize a static Boolean variable or a static set to keep track of whether the trigger logic has already been executed. This prevents the trigger from re-executing when it's not necessary. public class TriggerHandler {     private static Boolean isFirstRun = true;     public static void onBeforeInsert(List<MyObject__c> newList) {         if (isFirstRun) {             // Trigger logic             isFirstRun = false;         }     } } Use a Handler Class : Implement a separate handler class to encapsulate trigger logic. This class can have static variables to manage recursion and can be called from the trigger. public class TriggerHandler {     p...