Newer Version Available

This content describes an older version of this product. View Latest

Create a Sample DataSource.Provider Class Class

Now you need a class that extends and overrides a few methods in DataSource.Provider.

Your DataSource.Provider class informs Salesforce of the authentication and functional capabilities that are supported by or required to connect to the external system.

1global class SampleDataSourceProvider extends DataSource.Provider {

If the external system requires authentication, Salesforce can provide the authentication credentials from the external data source definition or users’ personal settings. This example specifies that the external system doesn’t require authentication, but also supports OAuth authentication. To do so, it returns AuthenticationCapability.ANONYMOUS and AuthenticationCapability.OAUTH in the list of authentication capabilities.

The getAuthenticationCapabilities method should always return the same list of authentication types regardless of user, org, or context.

1global override List<DataSource.AuthenticationCapability>
2        getAuthenticationCapabilities() {
3        // Best Practice: Always return a static list of authentication types
4        // Don't query the database, make callouts, or use dynamic logic
5        List<DataSource.AuthenticationCapability> capabilities =
6            new List<DataSource.AuthenticationCapability>();
7        capabilities.add(DataSource.AuthenticationCapability.ANONYMOUS);
8        capabilities.add(DataSource.AuthenticationCapability.OAUTH);
9        return capabilities;
10    }

This example also specifies that the external system allows SOQL queries, SOSL queries, Salesforce searches, upserting data, and deleting data.

  • To allow SOQL, the example declares the DataSource.Capability.ROW_QUERY capability.
  • To allow SOSL and Salesforce searches, the example declares the DataSource.Capability.SEARCH capability.
  • To allow upserting external data, the example declares the DataSource.Capability.ROW_CREATE and DataSource.Capability.ROW_UPDATE capabilities.
  • To allow deleting external data, the example declares the DataSource.Capability.ROW_DELETE capability.

The getCapabilities method should always return the same list of capabilities regardless of configuration or data.The returned capabilities should never change based on runtime conditions, user context, dynamic queries, or any other conditions.

1global override List<DataSource.Capability> getCapabilities() {
2        // Best Practice: Return a static list of functional capabilities
3        // Don't query the database, make callouts, or use dynamic logic
4        List<DataSource.Capability> capabilities = new
5        List<DataSource.Capability>();
6        capabilities.add(DataSource.Capability.ROW_QUERY);
7        capabilities.add(DataSource.Capability.SEARCH);
8        capabilities.add(DataSource.Capability.ROW_CREATE);
9        capabilities.add(DataSource.Capability.ROW_UPDATE);
10        capabilities.add(DataSource.Capability.ROW_DELETE);
11        return capabilities;
12    }

When you call the getAuthenticationCapabilities or getCapabilities methods, be sure the returned list always contains the same values. Never use a SOQL query, callout, or any conditional logic that changes the returned values based on runtime conditions. Returning varying lists of authentication capabilities or capabilities for an external system can lead to errors that are difficult to troubleshoot.

Warning

Lastly, the example identifies the SampleDataSourceConnection class that obtains the external system’s schema and handles the queries and searches of the external data.

1global override DataSource.Connection getConnection(
2        DataSource.ConnectionParams connectionParams) {
3        return new SampleDataSourceConnection(connectionParams);
4    }
5}