Answer: Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform’s server-side. It is used to perform operations that are too complex for standard declarative tools in Salesforce, such as creating triggers, batch jobs, and web services.
What is the difference between trigger.new and trigger.old in an Apex Trigger?
Answer:
trigger.new: Refers to the new versions of the sObjects that are being processed by the trigger. It is available in before insert, after insert, before update, and after update triggers.
trigger.old: Refers to the old versions of the sObjects that were previously in the database. It is available in before update, after update, before delete, and after delete triggers.
What is the use of the @isTest annotation in Apex?
Answer: The @isTest annotation is used to mark a class or method as a test class or test method in Apex. These methods do not count against the governor limits and are used to test your code in a controlled environment. Test methods must assert conditions to ensure that your code behaves as expected.
Explain the concept of Governor Limits in Salesforce Apex.
Answer: Salesforce imposes limits on the number of resources your Apex code can consume during execution. This ensures that no one transaction can monopolize system resources. Governor limits apply to things like the number of DML operations, the number of SOQL queries, and the number of records returned in a query.
What are the different types of collections in Apex?
Answer: Apex supports three main types of collections:
List: An ordered collection of elements.
Set: An unordered collection of unique elements.
Map: A collection of key-value pairs, where each key maps to exactly one value.
What is the difference between before insert and after insert triggers in Apex?
Answer:
before insert: Executes before a record is inserted into the database. It allows you to modify field values on the record before it’s saved.
after insert: Executes after a record has been inserted into the database. It’s useful when you want to perform operations that depend on the record being committed, such as sending emails or creating related records.
What are Batch Apex and when do you use it?
Answer: Batch Apex allows you to process large amounts of data asynchronously in smaller, manageable chunks (batches). You use Batch Apex when you need to process more than 50,000 records or when the operations in your code could exceed normal limits.
What is the difference between Queueable Apex and Batch Apex?
Answer:
Queueable Apex: Provides a simpler way to handle asynchronous processing. You can add jobs to the queue for future execution, and it is typically used for jobs that don’t require splitting into multiple batches.
Batch Apex: Allows you to process large data sets by breaking them into smaller chunks. It’s ideal for operations on large volumes of records, and it has more control over how the job is executed compared to Queueable.
What is a Future method in Apex?
Answer: A @future method is used to run operations asynchronously in the background. It is typically used for operations that don’t need to be completed immediately, like calling external web services or performing long-running computations.
What are the differences between @future, Queueable Apex, and Batch Apex?
Answer:
@future: Executes asynchronously, can be used for tasks like sending emails, making HTTP requests, or performing long-running tasks in the background.
Queueable Apex: Provides more flexibility than @future. It allows you to chain jobs and provides a more powerful job management framework.
Batch Apex: Allows for the processing of large datasets by breaking them into smaller chunks (batches). This is ideal for operations that involve more than 50,000 records or need precise control over job execution.
What is the difference between with sharing and without sharing in Apex?
Answer:
with sharing: Ensures that the sharing rules (access control) of the current user are respected, and the code respects the visibility of records based on the user’s profile or sharing rules.
without sharing: Ignores the sharing rules and allows access to records regardless of the user’s profile or sharing rules. It’s typically used for system-level operations.
Explain the concept of dynamic SOQL and dynamic Apex.
Answer:
Dynamic SOQL: Allows you to construct and execute a SOQL query dynamically at runtime. This is useful when you need to build queries based on user input or variable conditions.
Dynamic Apex: Refers to the ability to work with Salesforce metadata and dynamically work with objects, fields, and values using Apex code. It’s often used in scenarios where the object or field is not known at compile time.
How does the Database.executeBatch() method work in Apex?
Answer: The Database.executeBatch() method is used to execute a batch Apex job. It takes a batch class (which implements the Database.Batchable interface) and an optional batch size to specify how many records should be processed in each batch. It helps process large volumes of data asynchronously.
What are some best practices for handling errors in Apex?
Answer:
Use try-catch blocks to catch and handle exceptions.
Use custom exception classes to handle specific error scenarios.
Log errors using the System.debug() method or custom error logging mechanisms.
Use addError() method to display custom error messages to users.
Ensure proper exception handling in asynchronous processes to avoid silent failures.
Explain the concept of Custom Settings in Salesforce and their relation to Apex.
Answer: Custom settings are a type of data storage that can be used to store configuration data in Salesforce. They are similar to custom objects, but they are globally accessible and can be cached, providing better performance. In Apex, custom settings can be queried directly (using CustomSetting__c.getInstance()) to retrieve configuration values without needing to perform SOQL queries.
Trigger-Specific Apex Questions:
What are the different types of Apex Triggers?
Answer: Apex triggers are divided into two main categories:
Row-level triggers: These are executed once for each record in a DML operation, such as before insert, after insert, before update, after update, before delete, and after delete.
Statement-level triggers: These are executed once for the entire DML statement, such as before undelete, after undelete.
What is the purpose of using Trigger.isInsert, Trigger.isUpdate, and other Trigger context variables?
Answer:
Trigger.isInsert: Returns true if the trigger is fired during an insert operation.
Trigger.isUpdate: Returns true if the trigger is fired during an update operation.
Trigger.isDelete: Returns true if the trigger is fired during a delete operation.
These context variables help determine which type of operation (insert, update, delete) is occurring and ensure that specific logic is executed for each type of operation.
How can you handle large data volumes in Apex?
Answer:
Use Batch Apex for large data processing.
Avoid using nested loops or performing multiple DML operations within a loop to reduce the risk of hitting governor limits.
Use Selective Queries to retrieve only the necessary records and avoid retrieving unnecessary data.
What are some common governor limits you should be aware of in Apex?
Answer: Some common governor limits include:
100 SOQL queries per transaction.
150 DML operations per transaction.
200,000 records returned by a single SOQL query.
10,000 script statements (i.e., the number of operations executed in an Apex transaction).
What is the difference between Test.startTest() and Test.stopTest() in Apex testing?
Answer:
Test.startTest(): Marks the start of the portion of the test where governor limits are reset to their default values.
Test.stopTest(): Marks the end of the test portion, and any operations executed after it will count against the governor limits. This is used to ensure that the limits are only counted for the actual test execution code.