0% found this document useful (0 votes)
4 views24 pages

Understanding Queueables in Apex

The document provides a visual guide on using Queueables in Apex, particularly for callouts from triggers and chaining jobs. It explains how Queueables allow for asynchronous processing without blocking the main execution flow, detailing the implementation of the Queueable interface and the enqueueing process. Additionally, it highlights the importance of Queueables in managing long-running operations and their various use cases.

Uploaded by

rachita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Understanding Queueables in Apex

The document provides a visual guide on using Queueables in Apex, particularly for callouts from triggers and chaining jobs. It explains how Queueables allow for asynchronous processing without blocking the main execution flow, detailing the implementation of the Queueable interface and the enqueueing process. Additionally, it highlights the importance of Queueables in managing long-running operations and their various use cases.

Uploaded by

rachita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

QUEUEABLE

IN APEX
(visual guide)

Igor Kudryk [Link]


We usually use Queueables for

Callouts from triggers

Chaining Jobs

Polling Pattern

[Link] 2
Let’s say - you inserted new account

insert trigger

request

response

backup server

And you want to backup this account into


another server

[Link] 3
The API might take several minutes to
process

insert trigger

request

response

<10 minutes

That would block the whole org for


potentially many minutes

[Link] 4
So for that reason - we can’t make
callouts from triggers

... otherwise we would get this error

[Link] 5
Instead, you have to create a separate
queueable job

insert trigger execution

queueable execution

request
response

backup server

[Link] 6
The trigger execution will continue

insert trigger execution

queueable execution

request
response

backup server

[Link] 7
But a new job will perform the callout and
not block the main execution

insert trigger execution

queueable execution

request
response

backup server

[Link] 8
When the new job is created it is added
to the Queue

Job 2 Job 3 Job 1

... and executed whenever Salesforce


has resources

(order is not guaranteed)

[Link] 9
So... where
is code?

[Link] 10
Our class needs to implement the
Queueable interface

(code is simplified)

... and execute method

[Link] 11
In execute we write our main logic

(code is simplified)

“execute” is called by Salesforce when it


starts the execution of the job

[Link] 12
We usually don’t use this parameter

(code is simplified)

And this is our main logic - callout to


the backup server

[Link] 13
We still need to learn how to put this
queueable in the Queue

[Link] 14
System method to put the queueable
into the Queue

Instance of our queueable class

[Link] 15
Could also be multiple lines

And can have a delay 0..10 minutes


(in this example - 5 minutes delay)

[Link] 16
In the trigger, it could looks like this

[Link] 17
That’s this part of the execution

insert trigger execution

enqueue

queueable execution

request
response

backup server

[Link] 18
One of the applications
are chaning jobs

[Link] 19
It’s when we create a new queueable
instance from queueable

Job 1 Job 2 Job 3

It’s called chaining jobs, because they


will be executed in order

[Link] 20
You just need to enqueue a new job
from the queueabe

... it can be even the same queueable

[Link] 21
Those are only two use-cases for
Queueables

Callouts from triggers

Chaining Jobs

... but there are waaaaaay more of course

[Link] 22
Thank you for reading

[Link] 23
Reshare
this post
It‘s the best thing you can do to help others
understand Queueable in Apex

+ Follow
for more Apex stuff

Igor Kudryk [Link]

You might also like