Urla Shoes — Contact nationalization via async callout
Apex async-callout pattern: on Contact insert, a Queueable job hits the Nationalize.io API and writes the most likely country code back to the record.
- Year
- 2024
- Role
- Salesforce Developer
- Tech stack
- Apex TriggerTrigger HandlerQueueable ApexDatabase.AllowsCalloutsHTTP CalloutHttpCalloutMockRemote Site Setting
The problem
Salesforce triggers can't make HTTP callouts directly, but the business wanted Contact records enriched at the moment of creation. A naive solution would have used a synchronous flow action and broken under load.
The approach
After-insert trigger → ContactTriggerHandler collects the new Contact IDs and enqueues a single Queueable job marked Database.AllowsCallouts. The job calls api.nationalize.io with the FirstName, parses the JSON response, picks the country with the highest probability, and updates Nationalized_Country__c. Bulk-safe by design — one Queueable handles up to 200 contacts per trigger invocation.
The outcome
Full test coverage with HttpCalloutMock across six scenarios: success, empty response, HTTP 500 error, missing FirstName, bulk-insert of 10 contacts, and a direct unit test of the JSON parser. A clean reference implementation for any Apex trigger that needs to hit an external API.