Tuesday, January 10, 2023

Queueable Apex in Salesforce?

What is Queueable apex in Salesforce?

Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits: Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.

This Apex allows you to submit jobs for asynchronous processing similar to future methods. We have to implement the Queueable interface to make the class queueable. Implementation of classes and methods must be declared as public or global. We can add the job to the queue and monitor them by the going to apex job from the quick find box or from the job id by adding this interface. The interface enables you to add jobs to the queue and you can monitor them. This is an enhanced way of running your asynchronous Apex code compared to using future methods. 

The interface has only one method execute which takes one parameter of Queueable Context:

public void execute(QueueableContext context) { 
    // Your code here 

The important benefit of  Queueable interface methods is that some governor limits are higher than for synchronous Apex, such as heap size limits. 

Don't forget to check out: Queueable Apex vs Batch Apex | Salesforce Developer Guide

Queueable jobs are as similar to the future methods in that they’re both queued for execution, but they provide you with these additional benefits like:

  • This class can contain member variables of non-primitive data types, such as custom Apex types. All of those objects can be accessed when the job executes. 
  • One can chain one job to another job by starting a second job from a running job. These jobs are useful if your process depends on another process to have run first. 

 If the Apex transaction rolls back, any queueable jobs queued for execution by the transaction aren’t processed. 

An example is the implementation of the Queueable interface. The execute method in this example inserts a new account:

 public class AsyncExecutionExample implements Queueable { 

      public void execute(QueueableContext context) { 

          Account a = new Account(Name='Acme',Phone='(415) 555-1212'); 

               insert a;         

                    } 

                         } 

Check out another amazing blog by Saurabh here: Learn All About the Batch Apex in 2023

Call this method to add Job Id:

ID jobID = System.enqueueJob(new AsyncExecutionExample()); 

The job is added to the queue and will be processed when system resources become available. You can monitor the status of your job programmatically by querying AsyncApexJob or through the user interface in Setup by entering Apex Jobs in the Quick Find box, and selecting Apex Jobs. 

Perform a SOQL query on AsyncApexJob by filtering on the job ID that the System.enqueueJob method returns to query information about your submitted job. 

Refer By:

https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_queueable

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...