Apex Programming (Developer Path) – Complete Training Module - Textnotes

Apex Programming (Developer Path) – Complete Training Module


Apex is Salesforce’s object-oriented programming language, similar to Java, used for building custom automations and integrations.

MENU (Apex Topics)

  1. Apex Basics
  2. SOQL & SOSL
  3. DML Operations
  4. Apex Triggers
  5. Apex Classes
  6. Collections (List, Set, Map)
  7. Apex Best Practices (Governor Limits)
  8. Async Apex (Future, Batch, Queueable)
  9. Apex Testing (75% coverage)

1) Apex Basics (Syntax, Variables, Loops)

Title: Introduction to Apex

Description:

Apex is Salesforce’s object-oriented programming language, similar to Java, used for building custom automations and integrations.

Detailed Explanation:

  1. Strongly typed
  2. Object-oriented
  3. Runs on Salesforce servers
  4. Follows governor limits

Syntax Example:


public class HelloWorld {
public static void sayHello() {
System.debug('Hello Salesforce!');
}
}

Variables Example:


String name = 'Muni';
Integer age = 25;
Boolean isActive = true;

Loops Example:


for(Integer i=0; i<5; i++){
System.debug('Count: ' + i);
}

2) SOQL & SOSL

Title: Querying Salesforce Data

Description:

SOQL and SOSL retrieve data from Salesforce objects.

Detailed Explanation:

SOQL (Salesforce Object Query Language)

Used to query a specific object.

Example:


List<Account> accList = [SELECT Id, Name FROM Account WHERE Rating='Hot'];

SOSL (Salesforce Object Search Language)

Search across multiple objects.

Example:


List<List<SObject>> results = [FIND 'John' IN ALL FIELDS RETURNING Contact(Name), Account(Name)];

3) DML Operations

Title: Insert, Update, Upsert, Delete

Description:

DML performs operations that save or modify data in Salesforce.

Examples:

Insert:


Account acc = new Account(Name='ABC Corp');
insert acc;

Update:


acc.Rating = 'Hot';
update acc;

Delete:


delete acc;

Upsert:


upsert acc;

4) Apex Triggers

Title: Before & After Triggers

Description:

Triggers automate actions when records are created, updated, deleted, or undeleted.

Before Insert/Update Trigger

Used to validate or modify values before saving.

Example:


trigger AccountBefore on Account (before insert, before update) {
for(Account acc : Trigger.new){
acc.Description = 'Auto populated';
}
}

After Insert/Update Trigger

Used for operations requiring the record ID or external calls.

Example:


trigger OpportunityAfter on Opportunity (after insert) {
for(Opportunity opp : Trigger.new){
Task t = new Task(
Subject='Follow-up',
WhatId=opp.Id
);
insert t;
}
}

Trigger Patterns (Best Practice)

Title: Trigger Framework

Description:

Always use one trigger per object and call a handler class.

Pattern Example:

Trigger:


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

Handler Class:


public class AccountTriggerHandler {
public static void handleTrigger(List<Account> newList, Map<Id, Account> oldMap, TriggerOperation op) {
if(op == TriggerOperation.BEFORE_INSERT){
// logic
}
if(op == TriggerOperation.AFTER_UPDATE){
// logic
}
}
}

5) Apex Classes

Title: Writing Apex Classes

Description:

Classes contain reusable logic, functions, and business rules.

Example:


public class DiscountService {
public static Decimal calculateDiscount(Decimal amount){
return amount * 0.10;
}
}

6) Collections (List, Set, Map)

Title: Apex Collection Types

Description:

Collections store multiple values and are essential for bulk processing.

List Example:


List<String> names = new List<String>{'Ram','Shyam','Muni'};

Set Example:


Set<String> uniqueNames = new Set<String>{'AB','CD','AB'};

Map Example:


Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Name FROM Account]);

7) Apex Best Practices (Governor Limits)

Title: Writing Efficient Apex Code

Description:

Salesforce enforces limits to ensure performance.

Key Practices:

  1. Bulkify triggers
  2. Avoid SOQL inside loops
  3. Avoid DML inside loops
  4. Use maps & collections
  5. Use try-catch for error handling
  6. Callouts must be async

Bad Example (wrong):


for(Account acc : accList){
insert acc; // ❌ DML inside loop
}

Good Example (correct):


insert accList; // ✔ Bulk DML

8) Async Apex

Title: Asynchronous Programming in Salesforce

Description:

Async Apex runs long operations in the background.

Future Methods

Used for callouts & email sending.


@future
public static void sendEmailFuture(Id contactId){
// logic
}

Batch Apex

Used for processing large datasets.


global class MyBatch implements Database.Batchable<SObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id FROM Account');
}
global void execute(Database.BatchableContext bc, List<Account> scope) {
// logic
}
global void finish(Database.BatchableContext bc) {}
}

Queueable Apex

More flexible than future methods.


public class MyQueueable implements Queueable {
public void execute(QueueableContext qc){
// logic
}
}

9) Apex Testing (75% Code Coverage)

Title: Apex Test Classes

Description:

Salesforce requires minimum 75% code coverage before deployment.

Best Practices:

  1. Use @isTest
  2. Use Test.startTest() & Test.stopTest()
  3. Do not use real org data
  4. Create test data inside the test class

Example Test Class:


@isTest
public class DiscountServiceTest {
@isTest
static void testCalculateDiscount() {
Decimal result = DiscountService.calculateDiscount(1000);
System.assertEquals(100, result);
}
}