Sub topic 14
Building a Custom Adapter with the Apex Connector Framework
This chapter assumes you can read basic Apex, know how external objects and external data sources work, and have seen a Validate and Sync run. The earlier adapters (OData 2.0, OData 4.0, cross-org) cover most systems. When the external system speaks neither OData nor another Salesforce org, you write your own adapter with the Apex Connector Framework.
When a custom adapter makes sense
Build a custom adapter when the external system exposes a plain REST or SOAP API, when you need to combine several sources into one external object, or when you must reshape data before Salesforce sees it. If an OData endpoint exists, use it. A custom adapter means code you must test and maintain.
The two classes you write
A custom adapter has two parts. Each part extends a class in the DataSource namespace.
| Class | Extends | Job |
|---|---|---|
| Provider | `DataSource.Provider` | Declares supported authentication and capabilities, and returns a connection |
| Connection | `DataSource.Connection` | Describes the schema in `sync()` and returns rows from `query()` |
The provider
The provider tells Salesforce what your adapter can do. This one allows anonymous access and row queries only.
global class SampleDataSourceProvider extends DataSource.Provider {
override global List<DataSource.AuthenticationCapability> getAuthenticationCapabilities() {
return new List<DataSource.AuthenticationCapability>{
DataSource.AuthenticationCapability.ANONYMOUS
};
}
override global List<DataSource.Capability> getCapabilities() {
return new List<DataSource.Capability>{
DataSource.Capability.ROW_QUERY
};
}
override global DataSource.Connection getConnection(DataSource.ConnectionParams connectionParams) {
return new SampleDataSourceConnection(connectionParams);
}
}Capabilities control what the data source setup allows. With only ROW_QUERY, the external objects are read-only. To support writes, you would also declare the matching write capabilities and implement the upsert and delete methods in the connection.
The connection
The connection does the real work. The sync() method describes the external schema as tables and columns. Validate and Sync reads it to create external objects. The query() method returns rows.
global class SampleDataSourceConnection extends DataSource.Connection {
global SampleDataSourceConnection(DataSource.ConnectionParams connectionParams) {
}
override global List<DataSource.Table> sync() {
List<DataSource.Column> columns = new List<DataSource.Column>();
columns.add(DataSource.Column.text('ExternalId', 255));
columns.add(DataSource.Column.url('DisplayUrl'));
columns.add(DataSource.Column.text('Name', 255));
List<DataSource.Table> tables = new List<DataSource.Table>();
tables.add(DataSource.Table.get('Sample', 'Name', columns));
return tables;
}
override global DataSource.TableResult query(DataSource.QueryContext context) {
List<Map<String, Object>> rows = new List<Map<String, Object>>();
rows.add(new Map<String, Object>{
'ExternalId' => '1',
'DisplayUrl' => 'https://example.com/items/1',
'Name' => 'First item'
});
return DataSource.QueryUtils.process(context, rows);
}
}Every table needs the ExternalId and DisplayUrl columns. The DataSource.Table.get call takes the table name, the name field, and the column list.
The sample returns one hard-coded row. A real adapter would make an HTTP callout inside query() and map the response into the same row structure.
What QueryUtils.process does
The QueryContext carries the filters, sort order, and limit from the caller's SOQL. DataSource.QueryUtils.process applies them to your rows in memory. This is the easy path, but it has a cost: your code fetches rows first and filters afterward. For large sources, read the filter from the context and pass it to the external API, so only matching rows travel over the wire.
Connecting it to an org
- Deploy the two Apex classes.
- In Setup, open External Data Sources and create a new one.
- Set Type to Salesforce Connect: Custom and pick your provider class.
- Save, then click Validate and Sync and select the tables to create as external objects.
Exercise
Your adapter has ROW_QUERY as its only capability. A user opens a list view of the external object and tries to edit a record. What happens, and what would you add to change it?
Show answer
The edit fails because the external object is read-only. To allow edits, declare the write capabilities in the provider (for example, row create, update, and delete) and implement the matching methods in the connection class so they send changes to the external system.
Exercise
Why might DataSource.QueryUtils.process be a poor choice for a table with millions of rows?
Show answer
It filters, sorts, and limits the rows in Salesforce memory after your code has already fetched them. The external system sends everything, which is slow and can hit limits. Reading the filter from the QueryContext and passing it to the external API avoids this.