9.3 Test Data Factory Let’s create our test data factory for the given ContactTrigger test class; follow these steps: Step 1: Create the new class ContactFactory in your Developer Console. @isTest public class ContactFactory { public static Contact createContact(String firstName, String lastName) { Contact newContact = new Contact(); newContact.FirstName = firstName; newContact.LastName = lastName; return […]
9.2 Try-Catch Method In Apex, the Try-Catch method is used to handle exceptions that may occur during the execution of a block of code. It allows you to catch and handle specific types of exceptions, preventing your code from crashing and providing error handling capabilities. If you try to execute the code example as follows […]
9.1 How to Write Test Cases Unit tests in Apex are written using the Apex testing framework, which provides a set of classes and methods specifically designed for testing Apex code. These tests are typically written by developers to validate the functionality of their code and ensure that it meets the desired requirements. In Salesforce, […]
8.3 Implementing Security. stripInaccessible() Method This method allows you to filter records based on the permissions of the current user. It can be used to remove fields and objects that the user does not have access to across a set of records. It´s often used when dealing with collections of records like a list of […]
8.1 Execution Context In Apex, there are two types of execution contexts such as system execution context and user execution context. User execution context is when a user clicks a button or link, for example, that executes Apex code. When a system event or process executes Apex code, a system execution context is created. Apex […]
7.3 Bulk Processing for Large Datasets Collections like lists, sets, and maps are essential for bulk processing. They allow you to group and manipulate records efficiently. Fetch required data outside the loop and use collections to match and process records. Utilize bulk DML operations to update, insert, or delete multiple records in a single transaction. […]
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 […]