Advanced Apex & Edge Cases
What Are Edge Cases in Apex?
Short Description
Edge cases are uncommon but critical scenarios where Apex can fail, slow down, or behave unexpectedly if not handled carefully.
Simple Explanation
Edge cases are the corners of the road—most accidents happen there, not on straight paths.
Gist (Quick Revision)
Advanced Apex focuses on preventing rare but dangerous failures.
1. Dynamic DML
What Is Dynamic DML?
Dynamic DML allows you to perform DML operations on objects determined at runtime, not hardcoded.
Simple Explanation
Dynamic DML is like updating records without knowing the object name in advance.
Real-Life Example
A generic utility updates different objects based on configuration.
Code Example
String objectName = 'Account';
SObject obj = Schema.getGlobalDescribe()
.get(objectName)
.newSObject();
obj.put('Name', 'Dynamic Account');
insert obj;
When to Use Dynamic DML
-
Generic frameworks
-
Config-driven logic
-
Admin tools
Gist (Quick Revision)
Dynamic DML provides flexibility but must be used carefully.
2. Mixed DML Issues
What Is a Mixed DML Error?
Mixed DML occurs when you perform DML on:
-
Setup objects (User, Group)
-
Non-setup objects (Account, Contact)
in the same transaction.
Real-Life Example
Updating a User and Account at the same time.
Example That Causes Error
insert new Account(Name = 'Test'); insert new User(/* fields */); // Mixed DML error
How to Fix Mixed DML
Use asynchronous Apex or @future.
@future
public static void createUserAsync() {
insert new User(/* fields */);
}
Gist (Quick Revision)
Never mix setup and non-setup DML in the same transaction.
3. Long-Running Transactions
What Is a Long-Running Transaction?
A transaction that:
-
Uses too much CPU
-
Processes too many records
-
Hits governor limits
Simple Explanation
Doing too much work in one go slows everything down.
Real-Life Example
Processing thousands of records in a single trigger.
How to Handle Long Transactions
-
Break logic into smaller chunks
-
Use Batch Apex
-
Use Queueable Apex
-
Offload heavy work asynchronously
Code Strategy
System.enqueueJob(new HeavyProcessingQueueable());
Gist (Quick Revision)
Heavy logic belongs in async Apex, not synchronous triggers.
4. Heap Size & Recursion Issues
Heap Size Issues
What Is Heap?
Heap is the memory Apex uses during execution.
Common Heap Mistakes
-
Querying too many fields
-
Holding large collections
-
Nested loops with large data
Memory-Efficient Example
List<Account> accs = [SELECT Id, Name FROM Account]; accs.clear(); // free memory
Recursion Issues
What Is Recursion?
Recursion occurs when:
-
A trigger updates the same object
-
The trigger fires again and again
Real-Life Example
Two people calling each other continuously.
Safe Recursion Control
public class RecursionGuard {
public static Boolean isFirstRun = true;
}
if (RecursionGuard.isFirstRun) {
RecursionGuard.isFirstRun = false;
// logic
}
Gist (Quick Revision)
Manage heap carefully and always guard against recursion.
? Career Coach Advice (Interview-Ready)
Interviewers ask these topics to identify real-world experience.
Strong interview answer:
“I handle dynamic DML carefully, avoid mixed DML with async Apex, move heavy logic out of triggers, and protect against heap and recursion issues.”
That response signals senior Apex maturity.
✅ Final Takeaway
Advanced Apex is about preventing disasters before they happen.
Production-ready Apex anticipates edge cases.
