Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Salesforce Continuation Tips for Long-Running Async Callouts
1. Salesforce Continuation tip & tricks
Asynchronous callouts to make long-running requests
By AlanShen
20220502
https://www.ai-sfdc.com
2. Real world experience :
slow system
In this insurance company, the sales people(which owns salescloud licenses) need to
gather information based on customers’ statistics data, and send them to the remote
on-premise engine. The calculate engine is slow and the callout is long running also
maybe concurrent. When the answers are returned to salesforce, they should be further
manipulated automatically by apex for other business requirements.
3. System integration requirements summary:
• web service calls return slowly;
• lots of users requesting to a heavy weight external system in real-time;
• the response should be returned and fire other executions relating to the
perspective result;
One transaction one callout;
★We don’t need to concern or worry about the 20 concurrent limitation.
4. What is the 20 concurrent limit?
• “You can make up to 20 concurrent callouts to endpoints outside of
your Salesforce org’s domain. You can make unlimited concurrent
callouts to internal endpoints.”
https://developer.salesforce.com/docs/atlas.en-
us.198.0.apexcode.meta/apexcode/apex_callouts_timeouts.htm
• But,
https://salesforce.stackexchange.com/questions/102967/is-the-20-concurrent-callout-to-external-system-going-to-be-a-practical-issu
5. Salesforce Concurrent Long Running Apex Limit
• If you are making a sync call to a service, here is the limits: Number of
synchronous concurrent transactions for long-running transactions that last
longer than 5 seconds for each org is 10. This is called 10 by 5, so 11th call in
this situation will be denied. Doc :”If more transactions are started while
the 10 long-running transactions are still running, they’re denied.HTTP
callout processing time is not included when calculating this limit.”
• If an org has more than ten transactions in flight that have been going
for five seconds, that org is blocked from running any further Apex
transaction.
“HTTP Callout time DOES contribute to this limit despite the documentation
saying it doesn’t. Salesforce support confirmed this and so do the Apex
Execution event monitoring logs where one can see the CPU time being
milliseconds but the Callout Time being more than 5 seconds. ”c
alesforce Winter ’20 Release Notes
This limit only applied for http
callout in the past.
6. Now, long-running apex callout has been
excluded from the 5sec limit
• V54
https://developer.salesforce.com/docs/atlas.en-
us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatshee
t/salesforce_app_limits_platform_api.htm
Winter ’20 Release Notes
https://help.salesforce.com/s/articleView?id=release-
notes.rn_apex_request_limit.htm&type=5&release=222.
“However, HTTP callout processing time is no longer included when
calculating the 5-second limit. We pause the timer for the callout and resume
it when the callout completes.”
7. Are these asynchronous methods suitable?
• Queueable Apex (after you submit your queueable class for execution, the job is added to the queue, should be executed one by one;
Limits.getLimitQueueableJobs() Limits.getQueueableJobs() )
• Batch Apex (Maximum number of batch Apex jobs queued or active concurrently : 5)
• Scheduled Apex
• Future Methods (10 future calls per batch, Must be static and return void)
• Platform Events
• Asynchronous HTTP Callouts aka Continuations
• Async APIs
Apex allows HTTP and web service callouts from queueable jobs, if they implement
the Database.AllowsCallouts marker interface. In queueable jobs that implement
this interface, callouts are also allowed in chained queueable jobs.
Fire and Forget
Probably Fire and Forget
8. Why not choose Queueable Apex?
• “You could use a Queueable job to perform a callout that will be
executed asynchronously in the future, but you would lose the
context of the execution. Even if you get the job Id, your component
won’t have a callback that is executed once the job is finished.
Queueable jobs fit better in situations where the user doesn’t need to
wait to see the results of the callout. “ (ref 4)
9. Make Long-Running Callouts with Continuations
• On Spring’15, Salesforce delivered Continuations in Visualforce
• On Summer’19 they finally expanded to Aura and LWC
• Basically, a continuation will provide a callback mechanism so that
when the async callout is complete you can bring the result back to
the client.
• global timeout maximum of 120 seconds
10. How does continuation work?
Continuation pattern splits this in two parts:
• First: build up message payload; make callout.
• Second: examine response; manipulate data; return to the page.
What is happening between the end of method one and the start of method two?
In the new pattern, your thread is put to sleep
while we await the return of your callout.
Salesforce uses a separate server for the web
service request. This allows us to release the
thread resource, park transaction information
in serialized form, and reanimate it when the
response has returned. This means you can
have a thousand different people
simultaneously making callouts that last for 30
seconds. During those 30 seconds, they will
not be consuming threads. Thus, we won’t
need to block the org for long-running
transactions.
11. Callback method
• The callback method can also return a Continuation object. This
means you can make more than one callout in a single go, firing them
off simultaneously, and process the responses as they arrive. You can
even merge up to 3 Callouts in a single continuation object and fire
them in parallel fashion
Continuation.addHttpRequest(req1);
Continuation.addHttpRequest(req2);
Continuation.addHttpRequest(req3);
12. Sample 1 : Simplest Use
When the user hits Start Request button, the
callout is made to the URL. Once the response is
sent, processResponse method will be called.
statusBool variable will be true when the request
is hit and it shows the message. Once we get the
response, statusBool will be false and hide the
message.
13. But, Still challenges…
• Need to call 10+ API from Visualforce
• Cannot use chaining of Continuation Object as current chaining limit
is 3
• Cannot invoke each API from JavaScript remoting because it will be
considered as Synchronous Apex
14. Sample 2 : Call unlimited APIs asynchronously
from Salesforce
• Solution : Continuation Server in Salesforce with Continuation
Object and JavaScript remoting.
Ref 6
Governor Limit :A single Apex transaction can make a
maximum of 100 callouts to an HTTP request or an API call.
15. What Is JavaScript Remoting
• JavaScript remoting is a tool that front-end developers can use to
make an AJAX request from a Visualforce page directly to an Apex
controller. JavaScript remoting allows you to run asynchronous
actions by decoupling the page from the controller and to perform
tasks on the page without having to reload the entire page.
Ref 13
16. Comparing JavaScript Remoting
and <apex:actionFunction>
• The <apex:actionFunction> component also lets you call controller action
methods through JavaScript.
• In general, <apex:actionFunction> is easier to use and requires less code, while
JavaScript remoting offers more flexibility.
• Here are some specific differences between the two.
The <apex:actionFunction> tag:
• lets you specify rerender targets
• submits the form
• doesn’t require you to write any JavaScript
• JavaScript remoting:
• lets you pass parameters
• provides a callback
• requires you to write some JavaScript
Ref 13
17. Limitless Chaining of Continuation object
The con of this sample :
The callout are not
concurrnet
18. Sample 3 : Concurrent, Asnyc & Umlimited Callout
https://ebikes-yifengtech-developer-edition.gus.force.com/Continuation/
Set the wait time to 0 so all the
10 requests would be bulk
requested to the external server.
20. Continuation-Specific Limits : continued
• Up to three callouts per continuation. A single Continuation object can
contain a maximum of three callouts.
• Serial processing for continuation actions. The framework processes
actions containing a continuation serially from the client. The previous
continuation action call must have completed before the next continuation
action call is made.
• At any time, you can have only one continuation in progress on the client
DML operation restrictions
• Another limit is they only support up to a 1 Megabyte (MB) response size
whereas synchronous Apex callouts support up to 6 MB.
21. Callout Limits and Limitations
• https://developer.salesforce.com/docs/atlas.en-
us.apexcode.meta/apexcode/apex_callouts_timeouts.htm
22. Implementing and Testing Apex Continuations
• Apex Continuation from a Visualforce page – This covers the
implementation and testing aspect for non-static methods.
• Apex Continuation.from an Aura Components – This covers the
implementation of static methods.
• Apex Continuation from a Lightning Web Components – This covers
the implementation of static methods.