Learn Queueable Apex Like I am 10. Can Learning Be More Fun?

Learn Queueable Apex Like I am 10. Can Learning Be More Fun?

0f553db5-ace4-4e2d-9db8-604feeac37c5.png

Hello Friends, today we will learn Queueable Apex with Help of Dominos. Yay!!!

Domino's Pizza Serves Up A Slice of "the Noid" on NFTs - NFT Plazas

Let’s Get Started for the fun ride.

Think of Domino’s, your favorite place to order pizza. Imagine you and your friends are super hungry and decide to order a bunch of different pizzas.

 

216deffa259b5695d95ad160abe8052b.png

You call Domino’s and say, “I want a cheese pizza, a chicken pizza, and a veggie pizza.”

94dadb0646e5f80685b748f7229df21f.png

 

Dominos can’t make all your pizzas at the exact same time because there’s only one oven. So, they put your orders in a line, or a “queue.”

8e8876e4-2889-4e95-9d7f-a210c75a5059.png

First, they’ll make the cheese pizza, then the chicken, and finally the veggie pizza. Salesforce does the same thing with Queueable Apex. It takes all the jobs it needs to do and lines them up, so each one gets done in order.The Domino’s team starts with the cheese pizza, puts it in the oven, and while it’s baking, they get the next pizza ready. In Salesforce, Queueable Apex works on the first job in the queue. When it’s done, it moves to the next one, just like Domino’s making your pizzas one by one.

After each pizza is baked and boxed, it’s ready to be delivered to your house.

Safe Food Delivery

When Queueable Apex finishes the job, it’s like your pizza being ready and delivered to you – hot and fresh!!!

21c4e969-fe07-4981-bf8e-212b182daa07.png

Syntax of Queueable Apex

To use Queueable Apex, you first need to create a class that implements the ‘Queueable’ interface provided by Salesforce. Here’s a simple example to illustrate this:

A computer screen shot of a program Description automatically generated

In this example, ‘PizzaQueuable’ is a class that represents a task in our queue, similar to an order for a pizza at Domino’s. The ‘execute’ method is where the action happens – it’s like the process of putting your pizza in the oven and baking it.

We will understand in more detail in the next example.

How to Use Queueable Apex

To use this class to enqueue the job for creating a “Make Cheese Pizza” task, you would call the System.enqueueJob method with an instance of PizzaQueuable. Here’s how you would do that:

1. Open Developer Console

2. Open Execute Anonymous Window: In the Developer Console, go to “Debug” > “Open Execute Anonymous Window.”

3. Enqueue the Job: In the Execute Anonymous Window, enter the following Apex code to create an instance of PizzaQueuable class.

System.enqueueJob(new PizzaQueuable ());

4. Click the “Execute” button to run the code. This action enqueues the job, and Salesforce will process it asynchronously.

System.enqueueJob(new PizzaQueuable());

A screenshot of a computer Description automatically generated

This line of code is like calling Domino’s and placing your order.

Pizza Delivery Cartoon Stock Photos and Images - 123RF

Salesforce takes this order and puts it in its queue. The system will then process this order when it’s its turn, ensuring that each “pizza” (or task) is handled in the order it was received. Like Order 1, Then Order 2, Then Order 3 will be made.

Numbers Counting to 10 Collection Vol. 1" - Kids Learn to Count, Baby Toddler Songs, Nursery Rhymes - YouTube

Why Queueable Apex is Better than Future Method?

Future Methods: The One-At-A-Time Pizza Order

With future methods, it’s like you can only make one order at a time. After you order your cheese pizza, you have to wait and can’t order another pizza until the first one is delivered.

It’s simple:

1. Order Pizza- You call and order your cheese pizza.

2. Wait- You can’t do anything else. You just wait for the pizza to arrive.

3. Pizza Arrives- Yay! Eat pizza. 🍕

Chaining Jobs: The Ultimate Pizza Party Planner

Now, let’s say you’re having a party and you need lots of pizzas. With Queueable Apex, it’s like being able to call Domino’s and say, “I want a cheese pizza, then a chicken, and oh, also a veggie pizza,” all in one go. Domino’s will line up your orders and start making them one by one.

Here’s how it goes:

1. Order Pizzas- You list all the pizzas you want.

2. First Pizza Made and Next Pizza Prep – While they’re baking the cheese pizza, they’re also getting the chicken pizza ready.

3. Pizzas Arrive in Order – First the cheese, then the chicken, and finally the veggie. All coming to your party hot and fresh!

A computer screen shot of a program Description automatically generated

To run the ‘PizzaMakingQueue’ class we have created, use the ‘System.enqueueJob’ method.

System.enqueueJob(new PizzaMakingQueue(‘Cheese’));

Let’s Understand Above Code Line by Line

public class PizzaMakingQueue implements Queueable {

This line defines a new class named ‘PizzaMakingQueue’ that implements the ‘Queueable’ interface. Implementing this interface allows the class to be used for asynchronous processing in Salesforce.

Private Member Variable

private String pizzaType;

Here, a private variable named ‘pizzaType’ is declared. This variable is used to store the type of pizza that this particular job will make.

Constructor

public PizzaMakingQueue(String type) {

this.pizzaType = type;

}

This is a constructor for the ‘PizzaMakingQueue’ class. It takes a ‘String’ parameter named ‘type’, which represents the type of pizza to be made. The value passed to this constructor is then assigned to the ‘pizzaType’ member variable.

execute Method

public void execute(QueueableContext context) {

The ‘execute’ method is defined as part of the ‘Queueable’ interface. Salesforce calls this method when it’s time for the Queueable job to run. The ‘QueueableContext’ parameter provides the execution context for this job.

Creating a Task Record

Task pizzaTask = new Task();

pizzaTask.Subject = ‘Make ‘ + pizzaType + ‘ Pizza’;

pizzaTask.Status = ‘In Progress’;

pizzaTask.Priority = ‘Normal’;

insert pizzaTask;

These lines create a new Task record in Salesforce to represent the pizza-making process.

Decision Logic for Chaining Jobs

if (pizzaType.equals(‘Cheese’)) {

System.enqueueJob(new PizzaMakingQueue(‘Chicken’));

} else if (pizzaType.equals(‘Chicken’)) {

System.enqueueJob(new PizzaMakingQueue(‘Veggie’));

}

This conditional block checks the type of pizza being made. If the current job is making a Cheese pizza, it enqueues a new job to make a Chicken pizza. If it’s making a Chicken pizza, it enqueues a job for a Veggie pizza. This demonstrates job chaining, where the completion of one job triggers the start of another.

Finalizing the Last Job

else {

// Final pizza (Veggie) is being made, no more to enqueue

// Update the task to completed for the final pizza

pizzaTask.Status = ‘Completed’;

update pizzaTask;

}

In the case that the Veggie pizza is being made. there are no more jobs to enqueue. This code block updates the task’s status to “Completed,” indicating that the pizza-making process for this job is finished.

.

Non-Primitive Types: Customizing Your Pizza Order

Imagine you’re not just ordering a simple cheese pizza; you want a special pizza with all your favorite toppings, maybe some extra cheese, pineapple, and a sprinkle of oregano.

In Queueable Apex, having ‘non-primitive types’ is like being able to customize your pizza with any toppings you want.

 

d559ed11-0122-4b29-abbf-ba5002d7a256.png

You’re not limited to just saying “cheese” or “pineapple.” You can get specific about what you want, just like you can use complex data types (like sObjects or custom Apex types) to carry detailed instructions or data in your job.

Monitoring: Keeping an Eye on Your Pizza

After you place your order, Domino’s gives you an order number.

Online ordering food flat line concept vector spot illustration. Man buying pizza by smartphone 2D cartoon outline character on white for web UI design. Editable isolated color hero image 26622665 Vector Art

You can use this number to check the status of your pizza on their website or app. Is it being prepared, baked, or already on its way?

Domino's New Order Tracker Technology Is Ridiculous...In A Good Way

Similarly, when you submit a job in Salesforce using Queueable Apex by calling ‘System.enqueueJob’, Salesforce gives you back a job ID. This ID is like your pizza order number. You can use it to check up on your job in Salesforce, seeing if it’s queued, running, or completed, by looking at the Apex Jobs page or even querying the ‘AsyncApexJob’ record in Salesforce with that ID.

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

To check Status of Job. Go to Setup>Apex Jobs and Check the Status of the Job

 

So here is a Quick Summary

  • Queuable logic is written in class
  • Queuable allows chaining of jobs
  • Queuable Allows ID of Jobs to track progress
  • Queuable allows non – primitive types.

 

A yellow and orange text Description automatically generated

I hope you enjoyed the article. If you have any suggestions or feedback.

Feel free to connect with me on:

LinkedIn

Subscribe to my YouTube Channel

Follow my Blog – sfdcAmplified

 

 

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes