Integrations & APIs6 min read

Designing Idempotent Business Central API Integrations

Retries are normal; duplicate sales orders are not. A practical design for idempotent Business Central API integrations with an inbox and reconciliation.

Target BCBC 2023 Wave 2+ (v23+)
Runtime / StackAL Language v12.0+ / Custom API Pages
DeploymentCloud SaaS & On-Premise
Idempotent Business Central API integration cover

Most Business Central integrations fail on the second attempt, not the first.

The first request creates the sales order. The response is lost because the network drops, the caller times out, or the middleware restarts. The sender retries. Now there are two orders, two inventory reservations, and a finance team trying to work out which one is real.

The fix is not a better retry policy. The fix is an integration contract that makes the second request safe.

The retry that creates two orders

A retry is not an edge case. It is part of how distributed systems work. Any design that assumes every response arrives is a design that will eventually duplicate data.

There are four common failure points:

  1. The request reaches Business Central and commits, but the response never reaches the caller.
  2. The caller times out before the commit completes and retries while the first request is still running.
  3. A queue delivers the same message twice.
  4. A scheduled job reruns after a partial failure and replays the whole batch.

You cannot eliminate all four with a longer timeout. You need a durable record of what has already been accepted.

Idempotency is a data-model decision

An idempotency key is not a header that you generate and forget. It is a business identity that survives the request.

For an order intake integration, the key should usually be a combination of the source system and the source document ID:

  • Source system: SHOPIFY, CRM, WMS, EDI-PARTNER.
  • Source document: the order number or immutable GUID in that system.

Do not use the HTTP request ID alone. That ID changes on a retry. Do not use a hash of the entire payload unless the payload is guaranteed to be byte-identical on every retry; field ordering and optional metadata will break that assumption.

Store the key in Business Central before doing the work.

A workable inbox table

A small integration inbox gives you a place to record the decision, the payload identity, and the outcome.

table 50110 "Integration Inbox"
{
    Caption = 'Integration Inbox';
 
    fields
    {
        field(1; "Entry No."; Integer)
        {
            AutoIncrement = true;
        }
        field(10; "External System"; Code[30])
        {
            Caption = 'External System';
        }
        field(20; "External ID"; Code[100])
        {
            Caption = 'External ID';
        }
        field(30; "Status"; Option)
        {
            Caption = 'Status';
            OptionMembers = Received,Processing,Completed,Failed;
        }
        field(40; "Payload Hash"; Text[64])
        {
            Caption = 'Payload Hash';
        }
        field(50; "Received At"; DateTime)
        {
            Caption = 'Received At';
        }
        field(60; "Last Attempt At"; DateTime)
        {
            Caption = 'Last Attempt At';
        }
        field(70; "Error Message"; Text[2048])
        {
            Caption = 'Error Message';
        }
    }
 
    keys
    {
        key(PK; "Entry No.")
        {
            Clustered = true;
        }
        key(External; "External System", "External ID")
        {
            Unique = true;
        }
    }
}

The unique key on External System and External ID is the important part. It turns a duplicate request into a lookup instead of a new document.

The payload hash is a useful secondary check. If the same key arrives with a materially different payload, you have a conflict. Do not silently overwrite the first document; record the conflict and route it to an operator.

The status machine

A reliable integration has explicit states:

StateMeaningNext action
ReceivedThe request is stored and validated enough to process.Start processing.
ProcessingA worker owns the record.Do not start a second worker.
CompletedThe business document exists and the response is known.Return the original result on retry.
FailedProcessing stopped with a recorded error.Retry only after the error is understood.

A retry should return the original result when the state is Completed. It should not attempt to create the document again.

codeunit 50111 "Idempotent Order Intake"
{
    procedure ApplyOrder(ExternalSystem: Code[30]; ExternalID: Code[100]; Payload: JsonObject): Boolean
    var
        Inbox: Record "Integration Inbox";
    begin
        if Inbox.Get(ExternalSystem, ExternalID) then
            exit(Inbox.Status = Inbox.Status::Completed);
 
        Inbox.Init();
        Inbox."External System" := ExternalSystem;
        Inbox."External ID" := ExternalID;
        Inbox.Status := Inbox.Status::Received;
        Inbox."Received At" := CurrentDateTime();
        Inbox.Insert(true);
 
        // Process the payload in a controlled routine.
        // Mark Completed only after the business document commits.
        exit(true);
    end;
}

The real procedure should be more careful than this sketch: validate first, claim the record, use a transaction boundary that makes sense, and write the business document and the status update atomically where possible.

Handle the concurrent duplicate

Two workers can receive the same key at the same time. A lookup followed by an insert is not enough unless the unique key is enforced by the database.

Use both:

  • A unique key in the table.
  • A locking or claim step that only one worker can win.

In Business Central, keep the transaction short. Do not hold a lock while calling an external service. If the process needs to call out, split the work into a claim step, an external call, and a completion step, and make each step recoverable.

For API pages, return a stable response for a duplicate:

  • 201 Created for a new business document.
  • 200 OK with the existing document reference for a duplicate that is already complete.
  • 409 Conflict when the same key arrives with a different payload hash.
  • 422 Unprocessable Entity when the payload is invalid and should not be retried unchanged.

Your caller needs to distinguish "retry later" from "fix the payload".

Design for the response you cannot see

The integration owner should be able to answer these questions without reading application logs:

  • Which source document produced this Business Central record?
  • When was it first received?
  • How many attempts have been made?
  • What was the last error?
  • Is the business document complete, or only partially applied?
  • What should an operator do next?

A correlation ID that travels from the caller through the queue and into the Business Central record is worth more than a verbose log line that cannot be joined to anything.

Reconciliation beats optimistic retries

Retries handle transient failures. Reconciliation handles the cases where the two systems disagree about what happened.

Build a periodic job that compares:

  • Source documents marked as sent.
  • Integration inbox records.
  • Business Central documents.
  • Response status reported back to the source.

The output should be a small, explainable exception list, not a dashboard with a thousand red rows. A reconciliation job that nobody trusts is worse than no reconciliation job.

The checklist I use before go-live

  1. Every inbound message has a stable source system and source document ID.
  2. The unique key is enforced in the database, not only in application code.
  3. A duplicate returns the original result rather than creating a new document.
  4. The same key with a different payload is treated as a conflict.
  5. The status changes only after the business outcome is durable.
  6. Retries have a cap, a backoff, and a dead-letter path.
  7. The integration records a correlation ID and the last error.
  8. A reconciliation report can explain every exception.
  9. Payload retention follows a privacy and data-retention decision.
  10. A replay has been tested against a real duplicate, not only a happy path.

Idempotency is not a middleware feature you switch on. It is a property of the business process and the data model. Once it is designed in, retries stop being scary.

For the operational side of the same problem, the performance triage guide explains how to collect evidence when an integration starts slowing down the system.

Primary sources

Official references for this topic

Documentation and reference material from the vendor or standards body, kept alongside this article.

Full reference shelf
Wajahat Ali

Written by

Wajahat Ali

Senior Microsoft Dynamics 365 Business Central / NAV Techno-Functional Consultant with 6+ years of ERP delivery experience. Writing about Business Central, AL, integrations, reporting, and the work behind a stable go-live.

Related reading

Keep going on this thread

All articles
Chat on WhatsApp