Salesforce Custom adapter External Data Source

Introduction : –

In this blog, I am going to explain how to use Salesforce Apex connector framework to integrate your external data sources as salesforce external objects. In this example, I am using another salesforce org as a source org from where we are getting data for integration. Salesforce has cross-org  Adaptor out of the box but still, I am using integration through apex to the shown usage of the Apex connector framework.The Apex Salesforce Connect custom adapter can retrieve data from external systems and synthesize data locally. Salesforce Connect represents that data in Salesforce external objects, enabling users and the Lightning Platform to seamlessly interact with data that’s stored outside the Salesforce Org.Salesforce Connect provides seamless integration of data across system boundaries by letting your users view, search, and modify data that’s stored outside your Salesforce Org. In this example, I am going to show how to use apex connector framework to create, delete, update, search external data from the salesforce.In this example, we are going to sync book table data from one salesforce org to another salesforce as an external object by using apex connector framework. Here is the below image shows how to set up External Data Source by using apex connector adaptor .we are using named credentials for authenticating the salesforce source org.

 Using Apex Connector Framework

In order to create a custom adapter from the apex connector framework, you need to create two Apex classes: one that extends the DataSource.Connection class, and one that extends the DataSource.Provider class.DataSource.Connection class enables your Salesforce org to sync the external system’s schema and to handle queries, searches, and write operations (upsert and delete) of the external data.  DataSource.Provider to create a custom adapter for Salesforce Connect. The class informs Salesforce of the functional and authentication capabilities that are supported by or required to connect to the external system   DataSource.Connection class contains the flowing functions.


The sync () method is invoked when an administrator clicks the Validate and Sync button on the external data source detail page. It returns information that describes the structural metadata on the external system.Here is the code for sync method to create a table

Validate and Sync button which will produce the table as shown below.


The query method is invoked when a SOQL query is executed on an external object. A SOQL query is automatically generated and executed when a user opens an external object’s list view or detail page in Salesforce.


The search method is invoked by a SOSL query of an external object or when a user performs a Salesforce global search that also searches external objects. Because search can be federated over multiple objects, the DataSource.SearchContextcan have multiple tables selected. The following example allows you to search publisher id from the book table.



The upsertRows method is invoked when external object records are created or updated. You can create or update external object records through the Salesforce user interface or DML. The upsert operation is broken up into either an insert of a new record or an update of an existing record. These operations are performed in the external system using callouts. An array of DataSource.UpsertResult is populated from the results obtained from the callout responses.



The deleteRows method is invoked when external object records are deleted. You can delete external object records through the Salesforce user interface or DML.  The example uses the passed-in DeleteContext to determine what table was selected and performs the deletion only if the name of the selected table is Sample. The deletion is performed in the external system using callouts for each external ID. An array of DataSource.DeleteResult is populated from the results obtained from the callout responses.

Here is the complete code Connection  class 


Create DataSource.Provider 

The second class you need to create an apex custom adaptor is by extends the DataSource.Provider which provides the functional and authentication capabilities that are supported by or required to connect to the external system. the authentication capabilities indicated how do you want to authenticate the external system like Basic, OAuth, and Anonymous and functional capabilities like create, delete, insert and update, search etc…
Here is the complete code.


Now go to external data source from the setup menu and create a new external data source by selecting the “Salesforce Connect: Custom” Type .then validate and Sync to create a new external object. after creating the external object, you can able to create or update or search the global search.