Bulk REST Callouts with Queueable Apex in Salesforce

Bulk REST Callouts with Queueable Apex in Salesforce

On November 1, 2025, Posted by , In Apex, With Comments Off on Bulk REST Callouts with Queueable Apex in Salesforce
Bulk REST Callouts with Queueable Apex in Salesforce

Question:

We have a REST API callout implemented in Queueable Apex that fetches and updates HR information for related Contact records from an external system whenever a Contact is inserted or updated in Salesforce. The issue arises when a bulk insert or update operation occurs on Contact records, as this triggers multiple API calls and quickly hits governor limits. The external system does not provide a bulk API. How can we bulkify or redesign this integration so that it works efficiently without hitting limits?

Answer:

This problem occurs because each record update leads to an individual Queueable callout, which causes governor limit violations in bulk scenarios. Since the external system does not expose a bulk API, the optimization must happen on the Salesforce side by controlling how requests are batched and queued.

CRS Info Solutions offers expert Salesforce online training with real-time projects, certification guidance, interview coaching, and a job-ready approach. Enroll for free demo today!!!

One common approach is to collect all the Contact records being updated in a single Queueable or Batch process, and then process them in controlled chunks. This means grouping multiple record IDs into one Queueable execution, making a single API call per chunk instead of one per record. You can then loop through the response and update all corresponding records in Salesforce.

Here is a simplified example:

public class ContactHRIntegrationQueueable implements Queueable, Database.AllowsCallouts {
    private List<Id> contactIds;
    
    public ContactHRIntegrationQueueable(List<Id> contactIds) {
        this.contactIds = contactIds;
    }
    
    public void execute(QueueableContext context) {
        List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE Id IN :contactIds];
        
        // Example: Split into smaller chunks to avoid limits
        Integer chunkSize = 10;  
        for (Integer i = 0; i < contacts.size(); i += chunkSize) {
            List<Contact> chunk = contacts.subList(i, Math.min(i + chunkSize, contacts.size()));
            
            // Make a single REST callout for this chunk
            HttpRequest req = new HttpRequest();
            req.setEndpoint('callout:HRSystem');
            req.setMethod('POST');
            req.setBody(JSON.serialize(chunk));
            
            Http http = new Http();
            HttpResponse res = http.send(req);
            
            if (res.getStatusCode() == 200) {
                // Parse and update records
                Map<String, Object> hrData = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
                // Update logic goes here
            }
        }
    }
}

Explanation: This Queueable Apex class is designed to handle REST API callouts for multiple Contact records in bulk. It takes a list of Contact IDs, queries their details, and processes them in smaller chunks to avoid hitting governor limits. For each chunk, it makes a single POST callout to the external HR system, serializes the data into JSON, and then processes the response to update Salesforce records accordingly. This approach reduces the number of callouts and ensures the integration can scale for bulk operations.

Another approach is to use Platform Events or Change Data Capture (CDC). Instead of triggering immediate callouts for each DML, publish a platform event with the Contact IDs. A subscriber process (such as a Queueable or Batch Apex) can then aggregate these events over a short time window, combine them, and make fewer API calls.

If the external system has strict per-call limits but no bulk API, you can also consider building a middleware layer (like MuleSoft, AWS Lambda, or Heroku). Salesforce sends batched Contact IDs to the middleware, which then fans out and makes multiple calls to the external API. Middleware often provides more flexibility and avoids Salesforce’s synchronous limits.

Summing Up

This solution demonstrates how to manage bulk REST API callouts in Salesforce using Queueable Apex. By splitting Contact records into smaller chunks and processing them in batches, it prevents governor limit issues. The approach ensures scalable, efficient integration with external systems even when bulk operations occur.

Salesforce Training in Chennai: Unlock Your Potential

Elevate your career with our comprehensive Salesforce training in Chennai, designed to equip you with expertise in Admin, Developer, and AI modules. Our program offers unparalleled certification guidance, rigorous interview preparation, and industry-aligned training to ensure you gain a competitive edge. With in-depth modules and expert-led sessions, you’ll build a solid foundation in Salesforce while mastering advanced techniques to meet real-world demands.

Our unique learning approach combines practical hands-on sessions with detailed class notes, enabling you to gain job-ready skills and confidence. Whether you’re starting fresh or upskilling, our training ensures you stand out in the fast-paced Salesforce ecosystem.

Take the first step toward your success by joining our free demo class today and experience the best in Salesforce education!!!

Comments are closed.