Asynchronous Apex Flashcards
What is asynchronous apex?
It allows us to call a function and continue execution of the calling code while we wait for the function to complete so this allows parallel processing
How do implement Asynchronous Apex?
- @future annotation
- Queueable interface
- Database.Batchable interface
What is the difference between @future and queueable?
- Future
- Parameters need to be primitive
- cannot chain
- Queueable
- Parameters can be non-primitive (sObject or custom Apex Type)
- can chain up to one job
What methods must be included when writing a Batch class?
Start - used to collect records or object passed to interface method “execute” for processing
- Will return either a Database.QueryLocator object or a list of records/objects
- Governor limit
- GL for QueryLocator is bypassed and can go up to 50mil
- Whereas returning a list of objects is still 50,000.
Execute - performs the actual processing of “batch” of data.
- Takes in:
- A reference to the Database.BatchableContext object.
- A list of sObjects
Finish - used to execute post-processing operation (i.e. send email) and is called once all batches are processed
What is the order of Cron Expression?
Seconds 0-59
Minutes 0-59
Hour 0-24
Day of Month 1-31
Month 1-12
Day of Week 1-7
Year YYYY
* = Select all values
? = no specific value
What would this translate too 0 0 12 * * ?
Fire at 12PM every day
What is a Future Method?
Run in their own thread, and do not start until resources are available.
Typically used or callouts to external web services
@future annotation is added to the beginning of a static method
What is Queueable Apex?
Similar to future methods, but provide additional job chaining and allow more complex data types to be used.
- ID - we can use to track it’s execution
- Can contain member variables of non-primitive type
- sObjects or custom Apex type
- Can chain one job to another job
You can add jobs to the queue with System.enqueueJob();
When would you use Batch Apex?
Run large jobs that would exceed normal processing limits.
data caleansing or archiving records
When would you use Scheduled Apex?
When you want it to run at a specified time.
Daily or weekly tasks.