Advanced 35 min read

Integrate with an External Ledger

By the end of this lesson, you'll be able to:

  • Design a callout that reports transactions to an external ledger system
  • Handle callout failures without losing the underlying transaction
  • Apply Module 36's async-callout pattern to a compliance-driven integration

Prerequisites: "Enforce Governor-Limit-Safe Batch Processing"

Why this callout can't happen inside the trigger

Module 36 established that a trigger cannot make a synchronous callout at all — Salesforce throws a runtime error, since a trigger executes within an open DML transaction, and mixing that with a network callout risks leaving data in an inconsistent state if the callout hangs.

Queueable Apex, enqueued from the trigger

public class ReportToLedgerQueueable implements Queueable, Database.AllowsCallouts {
    private List<Id> transactionIds;

    public ReportToLedgerQueueable(List<Id> transactionIds) {
        this.transactionIds = transactionIds;
    }

    public void execute(QueueableContext qc) {
        for (Transaction__c t : [SELECT Id, Amount__c, Type__c FROM Transaction__c WHERE Id IN :transactionIds]) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint('callout:External_Ledger/transactions');
            req.setMethod('POST');
            req.setBody(JSON.serialize(new Map<String, Object>{
                'transactionId' => t.Id, 'amount' => t.Amount__c, 'type' => t.Type__c
            }));
            new Http().send(req);
        }
    }
}

TransactionTrigger enqueues this job (System.enqueueJob(new ReportToLedgerQueueable(ids))) instead of calling out directly — Module 38's exact Queueable pattern, chosen specifically because it can run after the trigger's transaction commits.

The transaction itself is never lost if the callout fails

Because the Transaction__c record is already committed by the time the Queueable job runs, a ledger-reporting failure (the external system being briefly down) doesn't roll back or lose the actual banking transaction — it's Module 39's "the payment isn't lost just because a downstream step failed" principle, applied here to compliance reporting rather than payment processing.

Exercise

As a comment, explain why ReportToLedgerQueueable is enqueued from the trigger rather than the callout being made directly inside TransactionTrigger.

Show hint

Think about what Module 36 established about triggers and callouts.

APEX

Integrate with an External Ledger Quiz

1. What happens to a Transaction__c record if its ledger-reporting callout later fails?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Many real banking systems must report every transaction to an external ledger or compliance system — this lesson applies Module 36's "callouts need to be async from a trigger" rule, since a Transaction__c insert cannot make a synchronous HTTP callout directly.