Salesforce-Platform-Developer-II Practice Test
202 Questions
Which use case can be performed only by using asynchronous Apex?
A. Querying tens of thousands of records
B. Making a call to schedule a batch process to complete in the future
C. Calling a web service from an Apex trigger
D. Updating a record after the completion of an insert
Explanation:
To determine which use case can only be performed using asynchronous Apex in Salesforce, let's evaluate each option based on the nature of synchronous and asynchronous processing in Apex.
Asynchronous Apex refers to operations that are executed in the background, separate from the main transaction, allowing time-consuming tasks to run without delaying the user experience or hitting governor limits. Examples include Batch Apex, Queueable Apex, Scheduled Apex, and Future methods. Synchronous Apex, on the other hand, executes immediately within the current transaction, such as in triggers or standard controller actions.
Option A: Querying tens of thousands of records
In Salesforce, SOQL queries in a synchronous context can retrieve up to 50,000 records within a single transaction, subject to governor limits. For example, you can use a SOQL query in a trigger or Visualforce controller to fetch tens of thousands of records synchronously, as long as the limit isn't exceeded. However, if the dataset exceeds 50,000 records or requires processing that might hit other limits (e.g., CPU time), asynchronous methods like Batch Apex, which uses a QueryLocator, become necessary. Since querying tens of thousands of records (assuming within the 50,000 limit) can be done synchronously, this use case does not require asynchronous Apex.
Option B: Making a call to schedule a batch process to complete in the future
Scheduling a batch process to run at a future time involves using the System.schedule method or implementing the Schedulable interface in Apex. This allows you to define a job that executes at a specified time, such as running a batch process overnight. By definition, scheduling a task to occur later is an asynchronous operation—there’s no way to "schedule" something to run in the future within a synchronous context, as synchronous code executes immediately. Other mechanisms, like time-based workflows, can’t schedule Apex batch processes; they are limited to actions like field updates or emails. Thus, this use case can only be performed using asynchronous Apex.
Option C: Calling a web service from an Apex trigger
Apex triggers are synchronous by default, executing immediately before or after a database operation. You can make a synchronous web service callout from a trigger using HTTP classes (e.g., HttpRequest), provided the callout completes within the transaction’s governor limits (e.g., 10 seconds of callout time). However, because callouts can be slow or unreliable, best practices recommend using asynchronous Apex (e.g., a Future method or Queueable Apex) to handle them, avoiding delays in the trigger. Despite this, synchronous callouts are technically allowed and possible, so this use case does not require asynchronous Apex.
Option D: Updating a record after the completion of an insert
This is a common scenario handled by an after insert trigger. When a record is inserted, an after insert trigger can execute synchronously to update the same record or related records within the same transaction. For example:
trigger MyTrigger on Account (after insert) {
List
for (Account a : Trigger.new) {
a.Description = 'Updated after insert';
accountsToUpdate.add(a);
}
update accountsToUpdate;
}
This operation is immediate and synchronous, completing before the transaction ends. Asynchronous Apex isn’t needed, as the update can occur in real-time within the trigger.
Conclusion:
A: Can be done synchronously up to 50,000 records, so asynchronous Apex isn’t required.
B: Scheduling a future batch process is inherently asynchronous and cannot be done synchronously.
C: Web service callouts from triggers can be synchronous, though asynchronous is preferred.
D: Updating after an insert is a synchronous trigger operation.
The only use case that can only be performed using asynchronous Apex is B. Making a call to schedule a batch process to complete in the future.
Salesforce-Platform-Developer-II Practice-Test - Home | Previous |
Page 5 out of 202 Pages |