Error & Debugging

Types of Errors in Salesforce (With Real Interview Scenarios)

By Apex Sikho · September 11, 2026

Types of Errors in Salesforce (Quick Recap)

Apex Errors

  • Compile-time errors
  • Run-time errors (Exceptions)
  • Logical errors

System Errors

  • Governor limit errors
  • CPU timeout
  • Heap size exceeded
  • Internal platform errors

LWC Errors

  • JavaScript errors
  • Template (HTML) errors
  • Apex call errors
  • Browser / runtime errors

Scenario 1: NullPointerException in Apex

Interview Question:

You are getting a NullPointerException in Apex. What could be the reasons and how do you fix it?

Answer: A NullPointerException occurs when we try to access an object or field that is not initialized.

Common causes:

  • SOQL query returning no records
  • Uninitialized sObject or list
  • Accessing relationship fields without checking null

Solution:

  • Always check for null
  • Check list size before accessing

List<Account> accList = [SELECT Id, Name FROM Account WHERE Name = 'Test']; if(!accList.isEmpty()) { System.debug(accList[0].Name); }

Interview Tip:

“NullPointerException is the most common Apex runtime error in real projects.”


Scenario 2: Governor Limit – Too Many SOQL Queries

Interview Question:

Your code fails with Too many SOQL queries: 101. What went wrong?

Answer: This happens when SOQL queries are written inside loops, violating governor limits.

Bad Code:

for(Account acc : accList){ Contact c = [SELECT Id FROM Contact WHERE AccountId = :acc.Id]; }

Optimized Code:

List<Contact> conList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accIds];

Interview Tip: “Bulkification is mandatory to avoid system limit exceptions.”


Scenario 3: DML Exception During Insert

Interview Question:

Insert operation fails with DmlException. How do you handle it?

Answer: DML exceptions occur due to:

  • Validation rules
  • Required fields missing
  • Duplicate rules
  • Permission issues

Proper Handling:

try { insert acc; } catch (DmlException e) { System.debug(e.getDmlMessage(0)); }

Best Practice: Use partial success with Database.insert(records, false).


Scenario 4: CPU Time Limit Exceeded

Interview Question:

Your batch or trigger fails with CPU timeout. What do you do?

Answer: CPU timeout happens due to:

  • Nested loops
  • Heavy string operations
  • Complex logic in triggers

Fixes:

  • Reduce nested loops
  • Move logic to async (Batch / Queueable)
  • Optimize conditions

Interview Line:“CPU issues are logic problems, not data problems.”


Scenario 5: LWC – Apex Call Fails but Works in Anonymous Apex

Interview Question:

Apex works in anonymous window but fails when called from LWC. Why?

Answer: Possible reasons:

  • Missing @AuraEnabled
  • Method not static
  • Permission / FLS issues
  • Exception thrown but not handled

Correct Method:

@AuraEnabled(cacheable=true) public static List<Account> getAccounts() { return [SELECT Id, Name FROM Account]; }


Scenario 6: LWC JavaScript Error – Undefined Property

Interview Question:

UI breaks with “Cannot read properties of undefined”. Why?

Answer: This happens when JS tries to access data before Apex response arrives.

Fix:

if(this.account && this.account.Name){ console.log(this.account.Name); }

Interview Tip: “Always assume async data may be undefined initially.”

Scenario 7: LWC Template Error – Missing Key

Interview Question:

Why is key mandatory in for:each?

Answer: key helps Salesforce track DOM elements efficiently. Without it, rendering issues occur.

<template for:each={list} for:item="item"> <div key={item.Id}>{item.Name}</div> </template>


Scenario 8: System Error – Internal Salesforce Error

Interview Question:

What do you do if you get an internal Salesforce error?

Answer:

  • Capture debug logs
  • Retry operation
  • Raise Salesforce support ticket

Important: Internal errors are platform-level, not code issues.