Apex Integration
Services in Salesforce
Callout or Web Service?
_ Key Concept
Every integration has two roles:
Client (Consumer)
1
the one making the request.
Server (Provider)
2
the one responding.
What is a Callout in Salesforce?
Definition:
A callout is when Salesforce acts as the client and makes a request
to an external system's API (REST or SOAP) to send or retrieve
data.
o Example:
Salesforce needs to check the current exchange rate by calling an
external banking API:
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.centralbank.com/rates');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
h Common use cases for callouts:
Pulling product data from ERP systems
Sending payment data to Stripe or PayPal
Verifying addresses, identity validation, etc.
What is a Web Service in Salesforce?
Definition:
A web service is when Salesforce acts as the server, and an external system
calls Salesforce to access its data or execute business logic.
o Example:
An invoicing system needs to fetch customer records stored in Salesforce.
< REST:
@RestResource(urlMapping='/customer/*')
global with sharing class CustomerService {
@HttpGet
global static Contact getCustomer() {
return [SELECT Id, Name, Email FROM Contact LIMIT 1];
}
}
< SOAP:
global with sharing class BillingService {
webservice static Account getCustomer(String name) {
return [SELECT Id, Name FROM Account WHERE Name = :name LIMIT 1];
}
}
h Common use cases for exposing web
services:
Allowing an external system to read/write Salesforce records
Building mobile apps that use Salesforce as backend
Sharing lead or opportunity data with partners
Quick Comparison Table
U Concept Callout from Web Service exposed
Salesforce by Salesforce
Who initiates? Salesforce External system
Who responds? External system Salesforce
Salesforce's role Client Server
Common scenario Salesforce fetches External app
data pulls/pushes
Salesforce data
Code style HttpRequest, @RestResource,
Http.send() webservice
Remote site config? ' Yes, in Remote o No
Site Settings
Summary
Callout = Salesforce calls an external service
Web Service = External service calls Salesforce
Module 1: Introduction to
Apex Integration
Learning Objectives:
Understand what callouts are and how they work
Know the difference between REST and SOAP
callouts
Set up Remote Site Settings to allow callouts
X Remote Site Settings
Configuration
Before making a callout, you must whitelist the external
endpoint:
1. Go to Setup > Remote Site Settings.
2. Click New Remote Site.
3. Enter a name and the external service URL.
4. Save.
Module 2: REST Callouts
from Apex
Learning Objectives:
Perform REST-based callouts from Apex
Handle responses and errors
Test callouts using mocks
Example: HTTP GET Callout
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.example.com/data');
request.setMethod('GET');
Http http = new Http();
HttpResponse response = http.send(request);
System.debug(response.getBody());
Example: HTTP POST Callout
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.example.com/data');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setBody('{"name":"John"}');
Http http = new Http();
HttpResponse response = http.send(request);
System.debug(response.getBody());
Testing Callouts Using Mocks
i
Mock responses let you test your logic without calling real
services.
Test.setMock(HttpCalloutMock.class, new MyMock());
Where MyMock implements HttpCalloutMock and simulates an
expected response.
Module 3: Making
SOAP Callouts from
Apex
Consume SOAP services using Apex
Generate classes from WSDL
Simulate SOAP responses in tests
Generate Apex Classes from
WSDL
1. Download the WSDL from the external service.
2. Go to Setup > Apex Classes > Generate from
WSDL.
3. Upload the WSDL and generate the
corresponding classes.
Example: SOAP Callout
MySoapService service = new MySoapService();
String result = service.getCustomerInfo('Acme
Corp');
System.debug(result);
Testing SOAP Callouts with Mocks
Test.setMock(WebServiceMock.class, new
MySoapMock());
MySoapMock simulates the external SOAP service
during test execution.
Module 4: Exposing REST
and SOAP Web Services
in Apex
Build custom RESTful APIs in Apex
Expose methods as SOAP web services
Exposing a Custom REST API
@RestResource(urlMapping='/accountinfo/*')
global with sharing class AccountREST {
@HttpGet
global static Account getAccount() {
return [SELECT Name FROM Account LIMIT 1];
}
}
Accessible via:
https://yourInstance.salesforce.com/services/apexrest/a
ccountinfo/
Exposing a SOAP Web Service
global with sharing class SoapExample {
webservice static String getHello(String name) {
return 'Hello ' + name;
}
}
Clients can generate a WSDL from this Apex class and
integrate easily.
Conclusion
' Understanding both sides of the integration (calling
external services and exposing Salesforce endpoints) is
fundamental for advanced Salesforce development. It
unlocks automation, real-time data sync, cross-
platform applications, and seamless enterprise
interoperability.
More Info:
Apex Integration Services