Apex Developer Track

Salesforce ApexInterview Questions

The definitive guide to Salesforce Developer interviews. Technical deep-dives, recruiter expectations, and production-grade answer strategies.

AI Overview & Core Concepts

Quick answers optimized for search engines, LLMs, and technical recruiters.

What is Apex in Salesforce?

Apex is a strongly typed, object-oriented programming language executed on the Salesforce multitenant platform. It allows developers to write transactional flow and control logic, execute DML actions, and create custom REST/SOAP web services. It compiles into bytecode and runs in a cloud environment governed by strict platform limits.

Apex Execution Rules

  • Bulk by Default: Triggers must be bulkified to process lists of up to 200 records without hitting governor execution limits.
  • No DML/SOQL in Loops: Never place database queries or update commands inside loops to prevent the SOQL 101 error.

Trigger Save Order vs. Database Saves

Before Triggers

Runs prior to database saving. Used to perform same-record validations and field updates in memory without DML overhead.

After Triggers

Runs post-saving. Provides access to system fields like Id or CreatedDate; used to perform DML on related child records.

Beginner Apex Questions

What is the difference between insert and Database.insert in Apex DML?

Weak Answer

"One is a keyword and the other is a method. They both do the same thing."

Strong Answer
The insert statement is "all-or-none"—if a single record in a list fails, the entire transaction rolls back. In contrast, Database.insert provides partial success handling via the optional allOrNone parameter. When set to false, valid records are committed, and failures return detailed logs in a Database.SaveResult[] array, allowing for custom error logging. To see how these DML choices impact your execution quotas, explore our comprehensive Governor Limits Explained guide.

Explain the difference between a Map and a Set in Apex.

Weak Answer

"A Map has keys and values, a Set is just a list with no duplicates."

Strong Answer
A Set is an unordered collection of unique elements, ideal for storing IDs for SOQL filters. A Map is a key-value collection providing O(1) lookup complexity. In bulkified code, Maps are essential to correlate records (e.g., mapping parent Account IDs to child Contacts) without using nested loops, which prevents CPU timeout issues.

What are Apex collections and how do you use them to write bulkified code?

Weak Answer

"Collections are lists of records that you loop through to update things."

Strong Answer
Apex supports three collection types: Lists (ordered, index-based), Sets (unordered, unique), and Maps (key-value pairs). Writing bulkified code requires using a Set to gather query criteria (like record IDs), executing a single SOQL query outside loops, and mapping the query results into a Map. This eliminates nested loops and limits SOQL/DML commands to a single execution. Read our Governor Limits Explained to see why this pattern is critical.

Intermediate Apex Questions

Why is it a best practice to enforce the 'One Trigger Per Object' pattern?

Weak Answer

"It makes the code easier to find and keeps the trigger folder clean."

Strong Answer
Salesforce does not guarantee the execution order of multiple triggers on the same object. Adhering to the "One Trigger Per Object" pattern by routing all events through a single Trigger Handler framework guarantees execution sequence, prevents recursive logic, simplifies maintenance, and enables programmatic bypassing. Check out our Apex Trigger Guide for structured implementation examples.

When should you write a 'Before' trigger versus an 'After' trigger?

Weak Answer

"Before is for validation, and After is for updating database records."

Strong Answer
Use "Before" triggers to validate records and update fields on the same record prior to database commit, as updates occur in memory and do not require a separate DML operation. Use "After" triggers when you require system-generated fields (such as record Id or CreatedDate) or when you need to perform DML operations on related objects.

What is asynchronous Apex, and when would you use a future method?

Weak Answer

"It runs in the background so users don't have to wait for the UI to load."

Strong Answer
Asynchronous Apex executes logic in a separate thread when resources are available, granting higher governor limits. An @future method is ideal for simple background execution, such as performing HTTP callouts or avoiding Mixed DML errors (updating setup and non-setup objects). However, future methods only accept primitive parameters (no sObjects) and cannot be directly chained.

Advanced Apex Questions

How does Queueable Apex improve upon Future methods, and how is it used?

Weak Answer

"Queueable is just a newer way of doing future methods and it lets you do callouts."

Strong Answer
Queueable Apex improves upon future methods by supporting complex data types (like sObjects and collections), returning a Job ID for tracking, and allowing jobs to be chained. Batch Apex is designed to process massive datasets (up to 50 million records) by automatically chunking the payload into blocks of 200 records, each with its own set of transaction limits. To test your knowledge on when to apply each model, practice with our real-time Salesforce Mock Interview simulator.

Explain the Enterprise Application Architecture (FFLIB) pattern in Apex.

Weak Answer

"FFLIB is a library that you install to make your code look clean and modular."

Strong Answer
Enterprise frameworks like FFLIB separate Apex logic into Service (business process orchestration), Domain (object validation and defaults), Selector (centralized SOQL queries), and Unit of Work (transactional DML registry) layers. This decouples responsibilities, enforces security defaults, eliminates redundant code, and enables pure unit testing by stubbing dependencies in-memory rather than relying on database inserts.

What is the Stub API in Apex, and how does it accelerate CI/CD testing?

Weak Answer

"It's an API to mock HTTP callouts so that your test classes pass successfully."

Strong Answer
The Stub API enables developers to build mock implementations of Apex classes at runtime by implementing System.StubProvider. This allows unit tests to intercept method calls and return mock data without writing records to the database. Bypassing database commits speeds up test execution significantly, accelerating CI/CD pipeline runs.

Scenario-Based Apex Questions

Scenario: A query on a large custom object is throwing a 'Non-selective query against large object' error. How do you fix it?

Weak Answer

"Add a LIMIT 1000 statement or check the fields in the where clause."

Strong Answer
A query becomes non-selective when it filters on a non-indexed field on a table containing more than 200,000 records, exceeding the optimizer's threshold (typically 10% for standard indexes, 30% for custom indexes). To resolve this, ensure filters utilize indexed fields (such as Id, Name, standard lookups, external IDs, or custom indexes requested from Salesforce Support). I'd verify using the Query Plan Tool in the Developer Console.

Scenario: You need to call a third-party REST API that takes 10+ seconds to respond. How do you design this?

Weak Answer

"Use a trigger to make the callout when the record is saved."

Strong Answer
Making synchronous callouts lasting over 5 seconds blocks transaction threads, exhausting the platform's limit of 10 concurrent requests. The correct design is asynchronous. For user actions, use a Continuation object in LWC, which suspends the thread while waiting for the response. For back-end updates, invoke a Queueable class. You can refine your integration design explanations on our Interactive Salesforce Mock Interview page.

Scenario: How do you prevent recursive trigger execution when updating related records?

Weak Answer

"Use a static boolean variable like runOnce and set it to false after the first run."

Strong Answer
A simple static boolean fails to prevent recursion during bulk DML transactions because records are processed in batches of 200, which resets the execution context but retains static variables. A bulk-safe design uses a helper class with a static Set<Id> to track processed records. The trigger handler compares incoming records against the set, filters out already processed records, executes logic, and adds the IDs to the set. Check our Apex Trigger Guide for trigger frameworks.

Frequently Asked Questions (FAQ)

What are Salesforce Apex Governor Limits, and why are they enforced?

Apex Governor Limits are runtime limits enforced by the multi-tenant architecture of Salesforce. Because multiple clients share the same server resources, these limits ensure that runaway Apex scripts do not monopolize CPU time, memory, database connections, or email bandwidth. Key limits include 100 SOQL queries per synchronous transaction and 150 DML statements. See the full table on our Governor Limits Explained page.

How does ForcePilot AI help developers prepare for Apex interviews?

ForcePilot AI offers a recruiter-grade simulation engine that dynamically tests your understanding of Apex. It evaluates your code logic, bulkification, and execution order awareness in real-time, providing immediate feedback on how to turn weak responses into strong ones. You can try a live session on the Salesforce Mock Interview screen.

What is the difference between Custom Settings and Custom Metadata Types?

Custom Settings are cached at the application level and are ideal for storing user-specific or hierarchical configurations. Custom Metadata Types allow you to define custom application configurations that can be packaged, deployed via change sets or the Metadata API, and queried without consuming SOQL limits.

What are the best practices for writing Apex test classes?

Salesforce requires a minimum of 75% code coverage to deploy Apex to production, but best practices dictate aiming for 100% logic coverage. Test classes should verify bulk behavior (processing 200+ records), use Test.startTest() and Test.stopTest() to reset governor limits and force asynchronous processing, and utilize mock interfaces (like the Stub API) to avoid database dependencies.

How do you handle CRUD and FLS security in Apex?

By default, Apex runs in "System Mode" which ignores sharing rules, object-level security (CRUD), and field-level security (FLS). To enforce security, use the with sharing keyword on classes to respect sharing rules. For CRUD and FLS, use the WITH USER_MODE database query filter, or filter results programmatically using Security.stripInaccessible().

Recruiter-Grade
Apex Mastery

ForcePilot AI adapts to your technical depth. Get real-time feedback on your code logic, bulkification strategies, and governor limit awareness.