How to Handle Bulk REST API Callouts?

Spread the love

Question

What is the best approach to manage existing REST API callouts in Salesforce when processing bulk records, particularly if the external system lacks support for bulk operations?

Answer

Handling REST API callouts for bulk records in Salesforce while adhering to governor limits and ensuring efficient processing requires rethinking the integration design. Below are multiple approaches to address the issue:

1. Batch Processing in Queueable Apex

One way to manage the governor limits is to divide the processing into batches of a manageable size. You can group the contact records and process each group in a separate Queueable Apex job. Here’s an example implementation:

public class BulkContactProcessor implements Queueable {
    private List<Contact> contacts;

    public BulkContactProcessor(List<Contact> contacts) {
        this.contacts = contacts;
    }

    public void execute(QueueableContext context) {
        for (Contact contact : contacts) {
            // Make the API call for each contact
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://example.com/hr-info');
            req.setMethod('POST');
            req.setBody(JSON.serialize(new Map<String, Object>{ 'ContactId' => contact.Id }));
            
            HttpResponse res = new Http().send(req);

            if (res.getStatusCode() == 200) {
                // Update the contact or related records as needed
                Map<String, Object> responseBody = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
                contact.Some_Field__c = (String) responseBody.get('SomeField');
            }
        }
        update contacts;
    }
}

public class ContactTriggerHandler {
    public static void processContactsInBatches(List<Contact> contacts) {
        Integer batchSize = 50; // Set batch size to stay within limits
        for (Integer i = 0; i < contacts.size(); i += batchSize) {
            List<Contact> batch = contacts.subList(i, Math.min(i + batchSize, contacts.size()));
            System.enqueueJob(new BulkContactProcessor(batch));
        }
    }
}

2. Leveraging a Custom Middleware or Orchestrator

If the external system does not support bulk operations, consider introducing a middleware solution. Instead of making individual API calls from Salesforce, send all the contact IDs as a single payload to the middleware. The middleware can sequentially process the records and interact with the external system.

Example middleware payload:

{
    "contacts": [
        { "id": "0031x00000XXXXXX", "otherField": "Value1" },
        { "id": "0031x00000YYYYYY", "otherField": "Value2" }
    ]
}

This approach minimizes the number of HTTP requests made by Salesforce and allows for more flexibility in handling processing logic.

3. Redesigning the Process Using Platform Events

Using Platform Events is another effective solution. When a contact record is inserted or updated, publish a Platform Event containing the necessary details. A separate service (middleware or a listener) subscribes to the event and processes the records, calling the external system as required. This decouples the API callouts from the Salesforce transaction.

Platform Event Example:

// Define Platform Event
public class HRUpdateEvent__e extends PlatformEvent {
    @AuraEnabled public String ContactId;
    @AuraEnabled public String OtherData;
}

// Trigger to Publish Events
public class ContactTriggerHandler {
    public static void publishHRUpdateEvents(List<Contact> contacts) {
        List<HRUpdateEvent__e> events = new List<HRUpdateEvent__e>();
        for (Contact contact : contacts) {
            events.add(new HRUpdateEvent__e(ContactId = contact.Id, OtherData = contact.SomeField__c));
        }
        EventBus.publish(events);
    }
}

4. Throttling API Requests Using Limits

You can implement logic to throttle the number of API requests within the limits of the current transaction. Store unprocessed records in a custom object or queue them for later execution in a separate transaction.

Example:

public class ContactProcessor implements Queueable {
    public void execute(QueueableContext context) {
        // Query unprocessed contacts from custom object
        List<Pending_Contact__c> pendingContacts = [SELECT Contact_Id__c FROM Pending_Contact__c LIMIT 50];
        for (Pending_Contact__c pendingContact : pendingContacts) {
            // API call logic here
        }
        // Delete processed records
        delete pendingContacts;
    }
}

Schedule the Queueable class to run periodically to process queued records.

Each approach has its pros and cons, depending on the specific requirements and constraints of your integration. If real-time processing is not mandatory, asynchronous solutions like middleware or scheduled jobs are more robust and scalable.

Summing Up

Handling REST API callouts for bulk records in Salesforce involves strategies to adhere to governor limits while ensuring efficient data processing.

  1. Batch Processing in Queueable Apex: Contacts are divided into smaller batches, and each batch is processed with separate API callouts using Queueable Apex. This approach ensures compliance with limits and efficient execution.
  2. Custom Middleware or Orchestrator: Middleware is introduced to receive and process bulk contact records. Salesforce sends all records in a single request, offloading processing and external API interaction to the middleware.
  3. Platform Events: Contact record changes are published as Platform Events, decoupling the callout logic from Salesforce transactions. A listener or middleware processes these events asynchronously and interacts with the external system.
  4. Throttling API Requests: Unprocessed contact records are queued in a custom object and processed periodically via Queueable Apex jobs. This limits the number of API callouts in each execution, ensuring adherence to governor limits.

These approaches offer scalable and flexible solutions for bulk record processing, even when the external system lacks bulk API support. The choice of solution depends on the integration’s real-time requirements and architectural constraints.

Master Salesforce with Expert Training

Our Salesforce Course offers a comprehensive learning experience, designed to help you master the Salesforce platform and launch a successful career. Whether you’re aiming for a role as a Salesforce Admin, Developer, or working with AI integrations, our courses cover all aspects of the platform in depth. With a focus on real-world projects, our training ensures that you gain hands-on experience and can apply your knowledge to solve practical business challenges. Led by industry experts, our program ensures you receive the skills and confidence you need to excel in the Salesforce ecosystem.

In addition to technical training, our Salesforce training in India also provides essential career support, including personalized mentorship and expert guidance on certification preparation. We prepare you for the job market with detailed class materials, real-time project work, and in-depth interview coaching. Our goal is to not only equip you with Salesforce expertise but also to ensure you’re ready to step confidently into your next role. By the end of the program, you’ll have the practical skills and knowledge necessary to succeed in Salesforce and accelerate your career.


0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open Chat
1
Dear Sir/Madam
How can I help you?