Top Salesforce Apex Interview Questions and Answers
1. What is Apex?
Apex is a programming language developed by Salesforce for writing custom code on the Salesforce platform.
2. Different types of collections in Apex?
Apex supports three types of collections: Lists, Sets, and Maps.
3. Tell me the difference between a List and a Set in Apex?
A List is an ordered collection that can contain duplicate values, while a Set is an unordered collection that does not allow duplicate values.
4. What is a trigger in Apex?
A trigger is an Apex script that runs before or after specific data manipulation language (DML) events, like insert, update, delete, etc.
5. Why we are using the “with sharing” keyword in Apex class definition?
The “with sharing” keyword enforces the sharing rules that apply to the current user.
6. What is a governor limit in Apex?
Governor limits are runtime limits enforced by Salesforce to ensure the efficient and secure execution of Apex code.
7. Name a few types of governor limits in Salesforce.
Some types of governor limits include limits on queries, DML operations, CPU usage, heap size, and callouts.
8. How can you handle exceptions in Apex?
You can handle exceptions in Apex using try-catch blocks. Exceptions can be caught using catch blocks and appropriate error-handling logic can be applied.
9. What is the difference between a Trigger and a Process Builder in Salesforce?
A trigger is an Apex script that runs on DML events, while Process Builder is a point-and-click automation tool to define processes without writing code.
10. How do you prevent hitting governor limits in your code?
You can prevent hitting governor limits by optimizing your code, using bulk processing, and adhering to best practices. Governor limit monitoring is essential during development.
11. What is the @AuraEnabled annotation used for?
The @AuraEnabled annotation exposes Apex methods to be called from Lightning Components.
12. How can you make a class or method globally accessible in Apex?
You can use the “global” access modifier to make classes or methods accessible outside of your application or namespace.
13. Explain the difference between a Trigger.old and Trigger.new in a trigger context.
Trigger.old represents the old version of records before they are updated, while Trigger.new represents the new version of records after they are updated.
14. What is the purpose of using the “Test” class in Apex?
The “Test” class is used for writing test methods to ensure that your Apex code works as expected.
15. How do you perform a callout to an external service in Apex?
You can perform callouts using the HTTP classes in Apex, such as Http and HttpRequest.
16. What is a Batch Apex class?
Batch Apex allows you to process large amounts of data in smaller chunks to avoid governor limit issues.
17. How can you implement a singleton pattern in Apex?
You can implement a singleton pattern by using a private constructor and a static instance variable in the class.
18. Explain the difference between a before trigger and an after trigger.
A before trigger fires before the data is saved to the database, while an after trigger fires after the data has been saved.
19. What is the purpose of using the “Database” class in Apex?
The “Database” class provides methods to perform DML operations, such as insert, update, delete, and upsert.
20. How can you schedule an Apex class to run at a specific time?
You can use the Salesforce scheduler to execute an Apex class at a specified time using the “Scheduler” class.
21. What is a future method in Apex?
A future method is used to perform asynchronous processing and is annotated with the “@future” keyword.
22. How can you prevent recursive triggers in Apex?
You can use a static variable or a static set to track whether the trigger has already run to prevent recursion.
23. What is the purpose of the “Limits” class in Apex?
The “Limits” class provides methods to retrieve information about the current Apex execution context’s governor limits.
24. What is the difference between a PageReference and a PageReference object in Apex?
A PageReference is used to navigate to a Visualforce page, while a PageReference object is used to manipulate URLs in Apex.
25. How can you debug Apex code in Salesforce?
You can use System.debug() statements to log debug information to the debug logs. Additionally, you can use the Developer Console or set up Debug Logs in Setup.