SlideShare a Scribd company logo
1 of 25
Download to read offline
Salesforce Continuation tip & tricks
Asynchronous callouts to make long-running requests
By AlanShen
20220502
https://www.ai-sfdc.com
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.
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.
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
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.
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.”
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
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)
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
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.
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);
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.
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
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.
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
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
Limitless Chaining of Continuation object
The con of this sample :
The callout are not
concurrnet
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.
Continuation-Specific Limits
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.
Callout Limits and Limitations
• https://developer.salesforce.com/docs/atlas.en-
us.apexcode.meta/apexcode/apex_callouts_timeouts.htm
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.
What does Asynchronous mean?
Ref 13
Additional point
Ref links:
(1) https://developer.salesforce.com/docs/atlas.en-
us.apexcode.meta/apexcode/apex_continuation_overview.htm?search_text=continuation%20server
(2) https://metillium.com/2020/10/salesforce-concurrent-long-running-apex-limit-troubleshooting/
(3) https://jefersonchaves.medium.com/continuation-on-lightning-web-components-8735b7f37fa1
(4) https://developer.salesforce.com/blogs/2020/05/apex-continuations-implementation-and-testing-in-aura-lwc
(5) https://www.jitendrazaa.com/blog/salesforce/design-continuation-server-in-salesforce/#more-6113
(6) https://www.jitendrazaa.com/blog/salesforce/limitless-chaining-of-continuation-object-in-salesforce/
(7) https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_continuations_limits.htm
(8) https://salesforce.stackexchange.com/questions/112654/how-to-work-around-the-100-callout-limitation
(9) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm
(10) https://www.linkedin.com/pulse/apex-continuations-what-when-why-veenesh-vikram-/
(11) https://www.infallibletechie.com/2018/04/continuation-class-example-in-salesforce.html
(12) https://salesforce.stackexchange.com/questions/239994/auraenabled-method-will-execute-in-synchronous-or-
asynchronous
(13) https://developer.salesforce.com/docs/atlas.en-
us.pages.meta/pages/pages_js_remoting_compare_actionfunction.htm
(14) https://pexlify.com/blog/apex-continuation
Github ref:
• https://github.com/victorgz/sfdc-apex-continuations
• https://github.com/jefersonchaves/lwc-continuation-examples
• https://github.com/mohan-chinnappan-n/continuation-server
• https://github.com/ccoenraets/lightning-component-apex-
continuation

More Related Content

Similar to Salesforce Continuation Tips for Long-Running Async Callouts

Mule soft mcia-level-1 Dumps
Mule soft mcia-level-1 DumpsMule soft mcia-level-1 Dumps
Mule soft mcia-level-1 DumpsArmstrongsmith
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.jsratankadam
 
Async event based web server
Async event based web serverAsync event based web server
Async event based web serverGi Woong Lee
 
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long PollingDifference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long Pollingjeetendra mandal
 
Service-Level Objective for Serverless Applications
Service-Level Objective for Serverless ApplicationsService-Level Objective for Serverless Applications
Service-Level Objective for Serverless Applicationsalekn
 
Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015Samuel De Rycke
 
Salesforce Integration Pattern Overview
Salesforce Integration Pattern OverviewSalesforce Integration Pattern Overview
Salesforce Integration Pattern OverviewDhanik Sahni
 
Arsitektur Aplikasi Modern - Faisal Henry Susanto
Arsitektur Aplikasi Modern - Faisal Henry SusantoArsitektur Aplikasi Modern - Faisal Henry Susanto
Arsitektur Aplikasi Modern - Faisal Henry SusantoDicodingEvent
 
Web Performance & Latest in React
Web Performance & Latest in ReactWeb Performance & Latest in React
Web Performance & Latest in ReactTalentica Software
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Technologies
 
Data power Performance Tuning
Data power Performance TuningData power Performance Tuning
Data power Performance TuningKINGSHUK MAJUMDER
 
Remote invocation
Remote invocationRemote invocation
Remote invocationishapadhy
 
Expect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservicesExpect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservicesBhakti Mehta
 
HBaseCon2017 Highly-Available HBase
HBaseCon2017 Highly-Available HBaseHBaseCon2017 Highly-Available HBase
HBaseCon2017 Highly-Available HBaseHBaseCon
 
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...Docker, Inc.
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorOrenEzer1
 

Similar to Salesforce Continuation Tips for Long-Running Async Callouts (20)

Mule soft mcia-level-1 Dumps
Mule soft mcia-level-1 DumpsMule soft mcia-level-1 Dumps
Mule soft mcia-level-1 Dumps
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.js
 
Async event based web server
Async event based web serverAsync event based web server
Async event based web server
 
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long PollingDifference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
 
Service-Level Objective for Serverless Applications
Service-Level Objective for Serverless ApplicationsService-Level Objective for Serverless Applications
Service-Level Objective for Serverless Applications
 
Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015
 
Oracle OSB Tutorial 2
Oracle OSB Tutorial 2Oracle OSB Tutorial 2
Oracle OSB Tutorial 2
 
Salesforce Integration Pattern Overview
Salesforce Integration Pattern OverviewSalesforce Integration Pattern Overview
Salesforce Integration Pattern Overview
 
Asynchronous apex
Asynchronous apexAsynchronous apex
Asynchronous apex
 
Arsitektur Aplikasi Modern - Faisal Henry Susanto
Arsitektur Aplikasi Modern - Faisal Henry SusantoArsitektur Aplikasi Modern - Faisal Henry Susanto
Arsitektur Aplikasi Modern - Faisal Henry Susanto
 
Web Performance & Latest in React
Web Performance & Latest in ReactWeb Performance & Latest in React
Web Performance & Latest in React
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Grails Services
Grails ServicesGrails Services
Grails Services
 
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
 
Data power Performance Tuning
Data power Performance TuningData power Performance Tuning
Data power Performance Tuning
 
Remote invocation
Remote invocationRemote invocation
Remote invocation
 
Expect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservicesExpect the unexpected: Prepare for failures in microservices
Expect the unexpected: Prepare for failures in microservices
 
HBaseCon2017 Highly-Available HBase
HBaseCon2017 Highly-Available HBaseHBaseCon2017 Highly-Available HBase
HBaseCon2017 Highly-Available HBase
 
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...
Infinit's Next Generation Key-value Store - Julien Quintard and Quentin Hocqu...
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactor
 

Recently uploaded

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
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.
  • 23. What does Asynchronous mean? Ref 13 Additional point
  • 24. Ref links: (1) https://developer.salesforce.com/docs/atlas.en- us.apexcode.meta/apexcode/apex_continuation_overview.htm?search_text=continuation%20server (2) https://metillium.com/2020/10/salesforce-concurrent-long-running-apex-limit-troubleshooting/ (3) https://jefersonchaves.medium.com/continuation-on-lightning-web-components-8735b7f37fa1 (4) https://developer.salesforce.com/blogs/2020/05/apex-continuations-implementation-and-testing-in-aura-lwc (5) https://www.jitendrazaa.com/blog/salesforce/design-continuation-server-in-salesforce/#more-6113 (6) https://www.jitendrazaa.com/blog/salesforce/limitless-chaining-of-continuation-object-in-salesforce/ (7) https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_continuations_limits.htm (8) https://salesforce.stackexchange.com/questions/112654/how-to-work-around-the-100-callout-limitation (9) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm (10) https://www.linkedin.com/pulse/apex-continuations-what-when-why-veenesh-vikram-/ (11) https://www.infallibletechie.com/2018/04/continuation-class-example-in-salesforce.html (12) https://salesforce.stackexchange.com/questions/239994/auraenabled-method-will-execute-in-synchronous-or- asynchronous (13) https://developer.salesforce.com/docs/atlas.en- us.pages.meta/pages/pages_js_remoting_compare_actionfunction.htm (14) https://pexlify.com/blog/apex-continuation
  • 25. Github ref: • https://github.com/victorgz/sfdc-apex-continuations • https://github.com/jefersonchaves/lwc-continuation-examples • https://github.com/mohan-chinnappan-n/continuation-server • https://github.com/ccoenraets/lightning-component-apex- continuation