Category: Asynchronous callout

All About Asynchronous Apex

While studying for the Salesforce Platform Developer II certification, I was going through the different options Apex provides to run Apex code asynchronously. This post is all about sharing the study notes with key points to remember.Apex provides four options to run code asynchronously. Future Method, Queueable Apex, Batch Apex and Scheduled Apex.Future MethodsMethods are annotated with @future annotation.Future methods should be static and can only return void type.It supports parameters, but parameters should be primitive data types, arrays of primitive data types, or collection. sObjects or objects cannot be used as parameters to future methods.These methods run in their own thread and will only start when the resources are available.They are used for long running operations and prevent any delay in apex transaction.They can be used for callouts to external web services and also separating DML operations to prevent Mixed DML error.Future methods should not be used for processing large amount of data. In that case, Batch Apex should be used.To test future methods, enclose test code between startTest and stopTest test methods.Future methods not necessarity execute in the same order as they are called.Future methods cannot be used inside Visualforce controllers getter, setter, constructor methods.Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htmhttps://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_future_methodsQueueable ApexApex code must implement Queueable interace.By implementing Queueable interface, Apex class can be added to the job queue and it will run when the system resources are available.The difference between Queueable Apex...

Read More

Continuation in Salesforce – Asynchronous Callout option for long running requests

While implementing features, we need to write code to call external web services to fetch or update information. But calling external web service doesn’t mean we should wait for the response from the web service. That is a bad design. We should always write code in such a way so that we can call external web service and still continue to do other work and when the response is available from the web service, we will update the page with response. This way of calling web service is known as Asynchronous callout.Since in Salesforce, we are living in a multi-tenant architecture, so calling external services synchronously can create issues if the response is taking too much of time to come due to network latency or complex calculation or long running requests. There can be n number of reasons why the response is taking longer time. To make sure System resources are available to all customers, Salesforce introduces limitation on how much synchronous callout you can do from a single organization. Limit is each org can make a maximum of 10 synchronous requests running for more than 5 seconds. When the limit is reached, any subsequent request will fail until and unless an existing request completes successfully.Apex continuation is an architectural design which will allow to call long running external web services in a scalable fashion without blocking any server...

Read More
Loading

Earlier Posts

  • 2024 (22)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)