To avoid recursive triggers in Apex, you can implement the following approaches as in the following example: \ 1.\ Static Boolean Variable: Create a static Boolean variable, and use it to track whether the trigger has already been executed. Set the variable to true before executing the trigger logic, and reset it to false after […]
Trigger.isBefore, Trigger.isAfter These context variables provide information about the timing of the trigger execution. These variables are contained in the System.Trigger class and are commonly used to control the flow of logic within triggers. •\ Trigger.isBefore: Returns true if the trigger was fired before any record was saved. This variable is often used to perform […]
Step 4: Go to Setup ➤ Object Manager tab ➤ Lead ➤ Triggers, and click the “New” button. trigger FieldChangeTracking on Lead (after update) { String fieldToTrack = ‘Rating’; Lead oldLead = Trigger.old[i]; Object_Id__c = newLead.Id, Field_Name__c = fieldToTrack, Old_Value__c = String.valueOf(oldLead.get(fieldToTrack)), New_Value__c = String.valueOf(newLead.get(fieldToTrack)) ); } } insert historyRecords; } } In this example: […]
Trigger.size The Trigger.size variable in Apex triggers is used to determine the total number of records in a trigger invocation, including both old and new records. It provides the count of records that caused the trigger to fire. Here are a few reasons why Trigger.size is commonly used in Apex triggers: •\ Bulk Processing: Apex […]
Imagine you want to enforce a validation rule during the conversion of leads in Salesforce. Specifically, you want to ensure that converted leads have a certain minimum lead rating before they can be converted. You can use the Trigger.new context variable to access the leads being converted and perform the validation. Go to Setup ➤ […]
Now when you insert new Contact records, this trigger ensures that the FirstName is set to “Max” and the LastName is set to “Blank” before the records are actually inserted into the Salesforce database. You can test the Apex trigger in your Salesforce environment by creating the new contact. Trigger events indicate when the trigger […]
7.1 Triggers and Their Execution Context Apex triggers are a powerful feature in Salesforce that allow you to execute custom logic before or after records are inserted, updated, or deleted in the database. They are associated with SObjects, which can be either standard objects such as Account, Contact, Opportunity, etc. or custom objects created to […]
Here’s an example to illustrate this approach. The code starts by defining a list of SObject types (searchTypes), which includes Account and Contact. It initializes a map (relatedRecordsMap) to store related records based on their SObjectType. List<SObjectType> searchTypes = new List<SObjectType> {Account.SObjectType, Contact.SObjectType}; Map<SObjectType, List<SObject>> relatedRecordsMap = new Map<SObjectType, List<SObject>>(); // Build the SOSL query […]
6.3 Create Queries with Best Practice SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) are two query languages used in Salesforce to retrieve data from the database. Here are the main differences between SOQL and SOSL: SOQL (Salesforce Object Query Language) –– Queries start with the “SELECT” keyword. –– It is used […]
6.2 Salesforce Object Search Language (SOSL) Salesforce Object Search Language (SOSL) is a query language specifically designed for searching records in Salesforce. It allows you to search across multiple standard and custom Salesforce objects simultaneously, making it a powerful tool for finding data that meets specific criteria. SOSL queries can be used in various contexts, […]