Salesforce Apex Interview Questions & Answers #salesforce #apex #interview
Summary
TLDRIn this fifth installment of the Apex interview preparation series, the host, Jen, addresses frequently asked questions about Salesforce's Apex programming. Topics include the limitations of future methods, such as the inability to pass SObjects and the restriction on chaining or scheduling them. The video also covers batch Apex, explaining its methods, the difference between using Database.QueryLocator and Iterable, and the importance of stateful and stateless batch jobs. Additionally, Jen discusses error handling and the key limits of batch Apex, providing practical insights for interview preparation and real-world application.
Takeaways
- 😀 The video is part of an interview preparation series focusing on Apex in Salesforce, addressing frequently asked questions from the audience.
- 🔍 The first discussed topic is the limitation of the `@future` method not supporting the SObject data type due to the potential for data changes during asynchronous execution.
- 🔑 A recommended workaround for the `@future` method's limitation is to pass the ID of the SObject and query the latest record within the method itself.
- ❌ It is not advised to pass SObjects through serialization and deserialization in `@future` methods, though it's mentioned as an alternative for scenarios where the SObject is guaranteed not to change.
- 🚫 The `@future` method cannot be called from another `@future` method or from a batch job due to Salesforce's restrictions on asynchronous operations.
- 🔢 The limit on the number of `@future` method invocations per transaction is 50, which is a key consideration to avoid hitting execution limits.
- 📅 Scheduled Apex does not support callouts, but there are workarounds involving the use of `@future` methods with `callout=true` or batch Apex.
- 🔬 Batch Apex is introduced as a method for processing large sets of records with limitations on the number of jobs that can be scheduled and executed.
- 🔄 Batch Apex has three key methods: `start`, `execute`, and `finish`, each serving a specific purpose in the batch process.
- 🔍 Batch Apex can be stateless by default, but the `Database.Stateful` interface can be implemented to maintain state across multiple transactions.
- 🚨 Error handling in Batch Apex can be achieved by implementing the `Database.ErrorPlatformEvent` interface, which is triggered automatically on errors or exceptions.
Q & A
Why doesn't the future method support the SObject data type?
-The future method in Salesforce does not support SObject data types because it is not guaranteed that the SObject passed as an argument will remain unchanged by the time the future method executes. To handle this, Salesforce recommends passing the ID and querying the latest SObject record within the future method.
What is an alternative way to pass SObject data to a future method if you are certain it won't change?
-As an alternative, you can pass the SObject by serializing it to a string and then deserializing it within the future method. However, this is not the recommended approach due to potential issues with data integrity and the complexity it introduces.
Can you call one future method from another future method?
-No, Salesforce does not allow chaining of future methods. Each future method must be invoked independently, and this is one of the limitations of using future methods.
Is it possible to call a future method from a batch job?
-No, Salesforce does not allow calling a future method from a batch job due to the risk of hitting the governor limits on the number of future method invocations per transaction.
What is the limit on the number of future method invocations per transaction?
-The limit on the number of future method invocations per transaction is 50. This is to prevent hitting the governor limits that could cause the transaction to fail.
Can you schedule a future method to execute periodically?
-No, future methods are designed for one-time asynchronous execution and cannot be scheduled to run periodically.
What are some key limitations of future methods that a developer should be aware of?
-Key limitations of future methods include: they are not suitable for processing large sets of records, they do not support chaining, they only support primitive data types, and they do not allow for programmatic tracing of their execution status.
What is the maximum number of jobs that can be scheduled at a given time in Salesforce?
-In Salesforce, you can schedule a maximum of 100 jobs at a given time.
How can you count the number of scheduled jobs in Salesforce?
-To count the number of scheduled jobs in Salesforce, you can query the `CronTrigger` object where the `JobType` is equal to 'S', which represents Scheduled Apex.
Can you write a sample Batch Apex class?
-A sample Batch Apex class should implement the `Database.Batchable<SObject>` interface and include the `start`, `execute`, and `finish` methods. The `start` method is responsible for collecting records, the `execute` method performs operations on these records, and the `finish` method is used for any post-processing tasks such as sending confirmation emails or submitting additional jobs.
What is the significance of the three methods in Batch Apex: start, execute, and finish?
-The `start` method is called at the beginning of the Apex job to collect records for processing. The `execute` method performs the actual operations on the collected records. The `finish` method is called after all batches have been processed and can be used for tasks like sending confirmation emails or submitting additional jobs.
What are the two return types for the start method in Batch Apex and when should each be used?
-The two return types for the start method in Batch Apex are `Database.QueryLocator` and `Iterable<SObject>`. The `Database.QueryLocator` is used when you need to query a large number of records directly from the database, up to 50 million. The `Iterable<SObject>` is used when you need custom control over how records are iterated and processed, such as when working with a custom collection of records.
What is the difference between using the Database.QueryLocator and the Iterable interface in Batch Apex?
-The `Database.QueryLocator` allows for querying up to 50 million records directly from the database, bypassing the standard SQL query limits. The `Iterable<SObject>` interface provides custom control over iterating and processing records, which is useful when working with custom collections or CSV files, but it is subject to the standard governor limits for SOQL queries.
What is the purpose of the Database.StateMachine interface in Batch Apex?
-The `Database.StateMachine` interface in Batch Apex is used to maintain state across multiple transactions within a batch job. This is useful when you need to share information or counts across batches, such as tracking the number of successful or failed records processed.
Can callouts be made within a Batch Apex job?
-Yes, callouts can be made within a Batch Apex job by implementing the `Database.AllowCallouts` interface. This allows the batch job to make external HTTP requests as part of its processing.
How is error handling implemented in Batch Apex?
-Error handling in Batch Apex is implemented by implementing the `Database.ErrorPlatformEvent` interface. This event is automatically fired whenever an error or exception occurs within the batch job, allowing you to handle errors in a centralized manner.
What are some key limits of Batch Apex that a developer should be aware of?
-Key limits of Batch Apex include: a maximum of five jobs can be queued or active at a time, a maximum of 50 million records can be processed if using `Database.QueryLocator`, a maximum of 10 callouts can be made from each of the start, execute, and finish methods, a maximum of 250,000 batch executions are allowed per 24 hours, and only one batch Apex start method can run at a time in an organization.
What is the default batch size for Batch Apex if the optional scope parameter is not used?
-The default batch size for Batch Apex, if the optional scope parameter is not specified, is determined by Salesforce and typically allows for efficient processing within the governor limits.
How can you execute a batch job from the developer console?
-To execute a batch job from the developer console, you would typically use the `System.enqueueJob()` method, passing in an instance of your batch class.
How can you execute a schedulable job from the developer console?
-To execute a schedulable job from the developer console, you would call the `System.schedule()` method with the appropriate parameters, such as the job's class and any required arguments.
Outlines
此内容仅限付费用户访问。 请升级后访问。
立即升级Mindmap
此内容仅限付费用户访问。 请升级后访问。
立即升级Keywords
此内容仅限付费用户访问。 请升级后访问。
立即升级Highlights
此内容仅限付费用户访问。 请升级后访问。
立即升级Transcripts
此内容仅限付费用户访问。 请升级后访问。
立即升级5.0 / 5 (0 votes)