Skip to main content

Apex Class

Class Definition: Defines methods and variables to perform actions in Salesforce.

public class MyClass {
    public Integer addNumbers(Integer num1, Integer num2) {
        return num1 + num2;
    }
}

Access Modifiers: Determines the visibility and accessibility of class members (public, private, global).

public class MyClass {
    private String name;
    public Integer age;
}

Static Methods: Methods that can be called without creating an instance of the class.

public class MathUtils {
    public static Integer add(Integer num1, Integer num2) {
        return num1 + num2;
    }
}

Constructor: Special method used for initializing objects of the class.

public class MyClass {
    private String name;
    
    public MyClass(String n) {
        name = n;
    }
}

Instance Methods: Methods that require an instance of the class to be called.

public class MyClass {
    public void displayMessage() {
        System.debug('Hello, World!');
    }
}

Apex Trigger

Trigger Syntax: Code that executes before or after specific data manipulation events occur on records.

trigger MyTrigger on Account (before insert) {
    // Trigger logic goes here
}

Trigger Context Variables: System-provided variables that provide context information for trigger execution.

trigger MyTrigger on Account (before insert) {
    for (Account acc : Trigger.new) {
        System.debug('New Account Name: ' + acc.Name);
    }
}

Trigger Events: Events that cause trigger execution (before insert, before update, after insert, after update, etc.).

trigger MyTrigger on Contact (after insert, after update) {
    // Trigger logic goes here
}

Trigger Handler Pattern: Best practice pattern for organizing and managing trigger logic.

public class AccountTriggerHandler {
    public static void handleBeforeInsert(List newList) {
        // Logic for before insert trigger
    }

    public static void handleAfterUpdate(List newList, Map oldMap) {
        // Logic for after update trigger
    }
}

Trigger Frameworks: Frameworks like Trigger Handler Framework to manage trigger logic efficiently.

trigger AccountTrigger on Account (before insert, after update) {
    AccountTriggerHandler.handleBeforeInsert(Trigger.new);
    AccountTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);
}

SOQL (Salesforce Object Query Language)

SELECT Statement: Used to query Salesforce records.

List accList = [SELECT Id, Name FROM Account];

Filters: WHERE clause used to filter records based on specific criteria.

List contacts = [SELECT Id, Name FROM Contact WHERE AccountId = :accId];

Relationship Queries: Queries that traverse relationships between objects.

List accList = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account];

Aggregate Functions: Functions like COUNT(), SUM(), AVG(), etc., used for summarizing data in queries.

Integer contactCount = [SELECT COUNT() FROM Contact];

Limitations: SOQL query limits enforced by Salesforce for governor limits.

List accList = [SELECT Id, Name FROM Account LIMIT 100];

DML (Data Manipulation Language)

INSERT: Used to insert new records into Salesforce objects.

Account acc = new Account(Name='Test Account');
insert acc;

UPDATE: Used to update existing records in Salesforce objects.

acc.Name = 'Updated Account';
update acc;

DELETE: Used to delete records from Salesforce objects.

delete acc;

UPSERT: Combines INSERT and UPDATE operations based on specified conditions.

upsert accList;

Exception Handling

try-catch Blocks: Used to handle errors gracefully in Apex code.

try {
    // Code that might throw an exception
} catch (Exception e) {
    // Handle the exception
}

System Exceptions: Built-in exceptions like NullPointerException, DmlException, etc.

try {
    // Code that might throw an exception
} catch (NullPointerException e) {
    // Handle null pointer exception
}

Custom Exceptions: User-defined exceptions for handling specific error scenarios.

public class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

Exception Methods: Methods like getMessage(), getStackTraceString(), etc., to retrieve information about exceptions.

try {
    // Code that might throw an exception
} catch (Exception e) {
    String errorMessage = e.getMessage();
}

Apex Annotations

@AuraEnabled: Allows Apex methods to be called from Lightning components.

public class MyController {
    @AuraEnabled
    public static String getGreeting() {
        return 'Hello from Apex!';
    }
}

@TestVisible: Makes private or protected variables and methods visible in test classes.

public class MyClass {
    @TestVisible
    private String secretData;
}

@Future: Marks a method as executing asynchronously at a later time.

public class MyFutureClass {
    @future
    public static void myFutureMethod(String input) {
        // Asynchronous logic
    }
}

@ReadOnly: Marks a method to execute in read-only mode, helpful for performance optimization.

public class MyReadOnlyClass {
    @ReadOnly
    public static List getReadOnlyAccounts() {
        return [SELECT Id, Name FROM Account];
    }
}

Apex Testing

Unit Tests: Tests that validate individual units of code in isolation.

@isTest
public class MyTestClass {
    @isTest
    static void testAddNumbers() {
        Integer result = MyClass.addNumbers(2, 3);
        System.assertEquals(5, result);
    }
}

Test Classes: Classes that contain unit tests for Apex classes and triggers.

@isTest
public class MyTestClass {
    // Test methods go here
}

System.assert Methods: Methods used to validate expected outcomes in unit tests.

System.assertEquals(expectedValue, actualValue);
System.assertNotEquals(unexpectedValue, actualValue);

Mocking: Simulating external dependencies or behavior in unit tests using mock objects.

@isTest
public class MyMockTestClass {
    @isTest
    static void testMock() {
        Test.setMock(HttpCalloutMock.class, new MyHttpMock());
        // Test logic using mock
    }
}

Subscribe For More Updates

 

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!