Integrations & Callouts in Apex
What Are Integrations & Callouts?
Short Description
Integrations allow Salesforce to communicate with external systems (payment gateways, ERP, banking systems, APIs).
A callout is how Apex sends requests outside Salesforce.
Simple Explanation
A callout is like making a phone call to another system and waiting for a reply.
Gist (Quick Revision)
Integrations let Salesforce talk to other systems using APIs.
1. REST Callouts
What Is a REST Callout?
A REST callout uses HTTP methods like GET, POST, PUT, DELETE to exchange data, usually in JSON format.
Real-Life Example
Salesforce sends customer data to a payment service and gets a success response.
Basic REST Callout Example
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.example.com/customers');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
When to Use REST
-
Modern APIs
-
Lightweight communication
-
JSON-based integrations
Gist (Quick Revision)
REST callouts are the most common and flexible way to integrate Salesforce.
2. SOAP Callouts
What Is a SOAP Callout?
A SOAP callout uses a WSDL file and structured XML messages.
Simple Explanation
SOAP is like filling out a fixed form—everything must follow strict rules.
Real-Life Example
Integrating Salesforce with legacy banking or ERP systems.
How SOAP Callouts Work
-
Upload WSDL
-
Salesforce generates Apex classes
-
Call methods directly
Example (Conceptual)
MySOAPService.Port service = new MySOAPService.Port(); service.someOperation();
REST vs SOAP (Easy Comparison)
| REST | SOAP |
|---|---|
| JSON | XML |
| Lightweight | Heavy |
| Flexible | Strict |
| Most common | Legacy systems |
Gist (Quick Revision)
Use SOAP only when the external system requires it.
3. Named Credentials
What Are Named Credentials?
Named Credentials securely store:
-
Endpoint URLs
-
Authentication details
Simple Explanation
Named Credentials are like a secure contact card for external systems.
Why Named Credentials Are Important
-
No hardcoded URLs
-
No passwords in code
-
Easier maintenance
-
Better security
Callout Using Named Credentials
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Payment_API/customers');
req.setMethod('GET');
Gist (Quick Revision)
Named Credentials simplify and secure integrations.
4. Authentication Strategies
Why Authentication Matters
Salesforce must prove its identity before accessing external systems.
Common Authentication Types
1. Basic Authentication
-
Username + Password
-
Simple but less secure
2. OAuth 2.0 (Most Common)
-
Token-based
-
Secure
-
Industry standard
3. API Keys / Tokens
-
Key passed in headers
-
Used in internal systems
Real-Life Example
Logging into an app using “Sign in with Google” → OAuth.
Best Practice
Use OAuth with Named Credentials whenever possible.
Gist (Quick Revision)
Authentication ensures secure communication between Salesforce and external systems.
