# How do asynchronous Apex callouts prevent user interface blocking during long-running external web service requests?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Asynchronous Callouts, Queueable Apex

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-asynchronous-apex-callouts-prevent-user-interface-blocking-during-long-running-external-web-service

## Answer

**Basics:** A synchronous callout holds the user's save or page load open until the remote service answers, up to the callout timeout. Triggers also cannot make callouts synchronously. Async Apex queues work to run later in its own transaction. Note that async here means background execution after the user transaction commits; the UI does not wait.

**How it works:**
1. The trigger or controller collects the record IDs and enqueues a job.
2. The user's transaction commits and returns.
3. Salesforce runs the job in a new transaction with its own limits (async CPU 60,000 ms, heap 12 MB).
4. The job re-queries current data, makes the callout, and writes the result back.
5. Failures are logged and optionally retried.

**Advanced / deep dive:** Options compared. **@future(callout=true)**: simple, static method, parameters must be primitives or collections of primitives, max 50 per transaction, cannot chain or be called from a future or batch. **Queueable**: accepts complex types, returns a job ID, can chain (only one child job from a running queueable; chain depth unlimited in production, 5 in Developer Edition/trials), up to 50 enqueued per synchronous transaction. **Batch** with `Database.AllowsCallouts` suits large volumes (each `execute` has up to 100 callouts). **Continuation** (Visualforce or `@AuraEnabled(continuation=true)` for Lightning) makes long-running callouts without holding an Apex thread, with up to 3 callouts per continuation. Remember the callout limits: 100 per transaction and 120 seconds cumulative. Async execution is subject to the org's 24-hour async allocation (250,000 or licenses times 200, whichever is greater). Use a Platform Event as an alternative decoupling mechanism where the subscriber does the callout.

**Code / config example:**
```apex
public class OrderSync implements Queueable, Database.AllowsCallouts {
    private Set<Id> orderIds;
    public OrderSync(Set<Id> ids) { orderIds = ids; }
    public void execute(QueueableContext qc) {
        List<Order__c> orders = [SELECT Id, Ext_Id__c, Status__c FROM Order__c WHERE Id IN :orderIds];
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:ERP_NC/orders/bulk'); // one bulk call, not one per record
        req.setMethod('POST'); req.setHeader('Content-Type', 'application/json');
        req.setBody(JSON.serialize(orders));
        HttpResponse res = new Http().send(req);
    }
}
// In the trigger handler (after update): System.enqueueJob(new OrderSync(changedIds));
```

**Hidden gotchas & must-remember points:**
- `@future` methods must be static and return void; no sObject parameters.
- Order of execution of async jobs is not guaranteed.
- Make the callout before any DML in the same transaction, or you get "uncommitted work pending".
- Tests need `Test.startTest()` and `Test.stopTest()` to run async code, and an `HttpCalloutMock`.
- An LWC calling Apex imperatively is still synchronous for that Apex call.
- Enqueued jobs can run seconds to minutes later; the UI should not assume completion.

**Best practice:** Prefer Queueable over `@future`, pass IDs and re-query, batch many records into one callout, and add retry and logging.

---
Source: ForceChallenges · last updated 2026-11-02
