Salesforce Apex Interview questions and answers in 2024 – Part I

Salesforce Shastras Salesforce Apex Interview questions and answers in 2024

This article aims to help you prepare for your Salesforce Interview, providing you with a detailed set of potential Salesforce Apex Interview questions and answers.

1. What is Apex?

  • Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform. It is used to write custom business logic and extend the functionalities of Salesforce.

2. How to handle exceptions in Apex?

   try {
       // Code that may throw an exception
   } catch (Exception e) {
       // Exception handling code

3. What is the difference between Trigger and Workflow in Salesforce?

  • Triggers are pieces of code that are executed before or after a record is inserted, updated, or deleted. Workflows are automated processes that can create tasks, send email alerts, or update fields based on specified criteria.

4. Explain the concept of governor limits in Salesforce.

5. How can you make a class or method accessible by only a specific group of users?

   public with sharing class MyRestrictedClass {
       // Code here
  1. How to handle bulk operations in Apex?
   Trigger MyTrigger on MyObject__c (before insert) {
       List<MyObject__c> recordsToUpdate = new List<MyObject__c>();
       for (MyObject__c obj : {
           // Perform bulk operations
       update recordsToUpdate;
  1. What is the difference between insert, update, upsert, and merge DML operations?
  • insert: Adds new records to the database.
  • update: Modifies existing records in the database.
  • upsert: Updates existing records or creates new records if they don’t exist.
  • merge: Combines duplicate records.

8. Explain the difference between a Set and a List in Apex.

  • A List is an ordered collection of elements, while a Set is an unordered collection that does not allow duplicate elements.

9. How can you implement a custom controller for a Visualforce page?

   public class MyController {
       public String message { get; set; }

       public MyController() {
           message = 'Hello from the controller!';

10. What is a future method in Apex?

  • A future method is an asynchronous method that runs in the background, allowing the calling Apex code to continue executing. It is annotated with @future and is often used for long-running operations or to perform tasks that should run separately from the main transaction.

11. What is the purpose of using a static variable or method in Apex?

In Apex, a static variable or method is associated with the class itself rather than with instances of the class.

Static Variable:

A static variable is shared among all instances of a class and retains its value throughout the lifetime of the application or until it is explicitly changed. It is often used for maintaining state information that needs to be common across instances of the class.


public class MyClass {
    public static Integer myStaticVariable = 0;

    public void incrementStaticVariable() {

Static Method:

A static method is a method that belongs to the class rather than to an instance of the class. It can be called using the class name, and it doesn’t require an instance of the class to be created.


public class MathOperations {
    public static Integer add(int a, int b) {
        return a + b;

Purposes and Use Cases:

  1. Shared State: Static variables allow you to maintain shared state information across instances of a class.
  2. Utility Functions: Static methods are often used for utility functions that don’t depend on the state of an instance.
  3. Constants: Constants, such as configuration values, can be declared as static variables.
  4. Counters and Trackers: Static variables are useful for counters or trackers that need to persist across multiple instances.
  5. Global Access: Static methods can be accessed globally without the need to create an instance of the class.
// Using the static variable and method
MyClass.myStaticVariable = 42;

// Using the static method
Integer result = MathOperations.add(10, 20);

In summary, static variables and methods in Apex provide a way to share data and behavior at the class level, rather than at the instance level. They are particularly useful when you want to maintain common state or have functionality that doesn’t depend on the specific instance of the class.

12. Explain the difference between a map and a list in Apex.

  • Answer: List is an ordered collection of elements, while a Map is a collection of key-value pairs where each unique key maps to a specific value.

13. How can you schedule an Apex job to run at a specific time?

String jobId = System.schedule('MyScheduledJob', '0 0 2 * * ?', new MyScheduledClass());

14. What is the purpose of the @testSetup annotation in Apex testing?

  • @testSetup is used to create test records once and share them across all test methods in a test class, reducing redundant data setup.

static void setupTestData() {
    // Create test records

15. How do you make a field read-only in Apex?

Schema.DescribeFieldResult fieldResult = MyObject__c.MyField__c.getDescribe();
fieldResult.isUpdateable(); // Returns true if the field is updateable

16. What is the purpose of the @future(callout=true) annotation in Apex?

  • It allows a future method to make a callout to external web services.
public static void myFutureMethod() {
    // Callout code here

17. How to prevent an Apex trigger from running recursively?

public class TriggerHandler {
    private static Boolean isExecuting = false;

    public static void myTriggerLogic() {
        if (!isExecuting) {
            isExecuting = true;
            // Trigger logic here
            isExecuting = false;

18. How can you query for all records modified in the last 7 days using SOQL in Apex?

List<MyObject__c> records = [SELECT Id, Name FROM MyObject__c WHERE LastModifiedDate = LAST_N_DAYS:7];

19. Explain the difference between a before trigger and an after trigger.

Answer: A before trigger is executed before the records are saved to the database, while an after trigger is executed after the records are saved.

Salesforce Trigger: In-depth Overview with Sample Code

20. How can you implement a batch job in Apex?

global class MyBatchClass implements Database.Batchable<SObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // Query for records to process
        return Database.getQueryLocator('SELECT Id FROM MyObject__c');

    global void execute(Database.BatchableContext BC, List<SObject> scope) {
        // Process records

    global void finish(Database.BatchableContext BC) {
        // Finalize processing

Resource : Apex Developer Guide

Subscribe to my YouTube channel for more exciting videos on salesforce 👇

Leave a Reply

Your email address will not be published. Required fields are marked *