Salesforce Asynchronous
Salesforce Asynchronous
64. How can avoid this Exception condition, without using try-catch?
We can update the trigger logic to leverage
the System.isFuture() and System.isBatch() calls so that the future method
invocation is not made if the current execution context is future or batch.
78.If I have written more than one System.enqueueJob call, what will
happen?
System will throw LimitException stating “Too many queueable jobs added
to the queue: N”
79.What are the Limitations of Queueable Jobs?
50 jobs can be added to the queue with System.enqueueJob() method in a
single transaction
Maximum depth of chain job is 5 i.e., 4 child jobs and initial parent jobs for
Developer and Trail organizations but there is no limit in other editions
108. Give me a real time scenario where you have used batch apex?
1. Start Method:
o Query opportunities that have been updated in the last 24
hours.
o Store these opportunities in a list to be processed.
2. Execute Method:
o Process each batch of opportunities.
o For each opportunity, make a callout to the external system to
update the record.
o Track successful and failed callouts.
3. Finish Method:
o Send a summary email report to the system administrator,
detailing the number of successful and failed updates.
Governor Limits Considerations
Heap Size Limit: The heap size in a batch job can quickly be exceeded if
handling large volumes of data.
1.Data Loader is primarily a tool for manual data import/export and requires
human intervention to run the process. For a nightly synchronization task,
automation is crucial to ensure timely and consistent updates.
- The Queueable Apex class processes each order record, preparing the
necessary data for the external API.
- For each order, the class makes an API callout to 'ShipStation' to update
the order details.
Now you might be thinking why we will use Queueable Apex Instead
of Batch or Future Apex here?
Iterable is Suitable for scenarios where data comes from various sources,
including non-Salesforce databases, external systems, or complex
collections in memory. It offers flexibility in defining custom logic for data
retrieval. It provides more control over the batch size and is limited to
processing up to 50,000 records.
global Iterable<Account> start(Database.BatchableContext BC) {accounts
= [SELECT Id, Name FROM Account WHERE CreatedDate =
LAST_N_DAYS:30];
115. What are some real-world scenarios where you might use Batch
Apex?
Yes, the order of batch jobs in the Apex Flex Queue can be changed, but
only for jobs with the status "Holding". This can be done either through the
UI of the Apex Flex Queue or using the Apex Flex Queue methods.
The scope parameter in Batch Apex defines the number of records that are
passed to the execute method at one time. It helps in managing governor
limits by breaking down large datasets into smaller, more manageable
chunks for processing. The maximum value for the scope parameter is
2,000 records.
119. How to handle errors in Batch Apex?
It is possible to call Schedulable Apex from a Future Method, and you can
schedule up to 100 jobs.
121. What happens if more than 50 future method calls are made in a
transaction?
122. Can you call a Batch Apex job from a Future Method?
No, Batch Apex cannot be called from a Future Method due to restrictions
on mixing asynchronous operations in Salesforce.
Yes, but you are limited to enqueuing only one Queueable job per future
method invocation.
Future Methods cannot directly pass complex data types like SObjects.
Instead, pass IDs or primitive types and query the necessary data within
the Future Method to ensure accuracy.
127. What are the governor limits for Future Methods in Salesforce?
For handling large volumes of data, Batch Apex is most suitable because it
allows processing in smaller chunks (batches), avoids governor limits, and
can handle up to 50 million records. It also provides methods to track the
status and results of batch jobs.
131. You need to deactivate user accounts and reassign their active
cases to a new owner. These operations involve setup and non-setup
objects, which could lead to a mixed DML error. How would you
handle this scenario?
To avoid mixed DML errors, you can use Future Methods. The user
deactivation can be handled asynchronously using a future method, while
the reassignment of cases can be done in the synchronous part of the
transaction.
1. First Job (Data Validation): Implement a Queueable Apex class for data
validation. At the end of the execute method, enqueue the next job.
2. Second Job (Score Calculation): The next Queueable Apex job
calculates the customer scores. This job is enqueued at the end of the first
job. Once done, it enqueues the final job.
3. Third Job (Notification Sending): The final Queueable job sends
notifications based on the results of the score calculations.
Sample Code:
public class ValidateDataJob implements Queueable {
public void execute(QueueableContext context) {
// Data validation logic
// Chain the next job
System.enqueueJob(new CalculateScoreJob());
}
}
Sample Code
public class UserManagement {
public void transferOwnershipAndDeactivate(User user) {
// Transfer ownership of non-setup objects (e.g., Account)
transferAccountOwnership(user.Id);
// Call future method to deactivate user
deactivateUserAsync(user.Id);
}
@future
public static void deactivateUserAsync(Id userId) {
User user = [SELECT Id, IsActive FROM User WHERE Id = :userId];
user.IsActive = false;
update user;
}
1. Schedulable Apex for Scheduling: Schedule the Batch Apex job to run at
a specific time each night.
2. Batch Apex for Data Export: Query the relevant records, process them in
chunks, and perform the data export.