As you might already know, using Salesforce Connect, we can display external data in Salesforce without physically creating record. Before Salesforce Connect, we had few options like Visualforce, Canvas etc. There are few options available in Salesforce Connect to show data like using protocols OData 2 or OData 4, cross org adapter , custom Apex adapter etc.
There could be scenario, like you already have license for Salesforce connect and want to use it to expose external data inside Salesforce. If you think about any custom solution using Lightning component or Visualforce, there could be many considerations and most important would be displaying data on user interface.
Using Salesforce Connect in above scenario will cut down your most of effort. Your data would be exposed as External Object and you can use it just like custom object. Just imagine, how cool it would be that you would not need to write a single line of code for data presentation.
Now, here your challenge comes. It is pretty much possible that external data does not support OData protocol. And you don’t have necessary middleware tool available to perform transformation and expose it as OData. It doesn’t mean that you cannot use Salesforce Connect. One of the feature of Salesforce Connect is writing and using Custom Adapter using Apex.
Lets have a fun and build very simple Custom Adapter for Salesforce Connect using Apex.
For this blog post, we would be using this free API which returns JSON of healthcare blogs in Spanish and English language. There is no need of any kind of authentication in this example, which will make it a lot simpler.
First, we will need to create a class which extends DataSource.Connection and then define sync method. This method would have information about schema for all table and columns available to Salesforce Connect.
Next, we would need to override query method. This would be executed by Salesforce connect whenever any SOQL issued against this external object or list view or detail page is viewed. Now, this is the method where most of logic would be executed. Calling external API, parsing it to return as Database.TableResult format. Typically, searching, sorting or pagination should be implemented at external server. However, in our case healthcare API does not has any capability to search, sort or perform pagination. And in-fact, it worked in favor of our post so that I can demonstrate how Salesforce DataSource.QueryUtils class can be used to filter, sort and implement pagination. Would like to remind that DataSource.QueryUtils are good for demo purpose however you should not use it in production.
Now, all left is to create a Provider class by extending DataSource.Provider. This class will inform Salesforce Connect that which authentication mechanism is supported and what are the capabilities. Capability includes ability to create, update, delete, view or search record. Complete source code for this post is available below