Newer Version Available
Create a Sample DataSource.Connection Class
1global class SampleDataSourceConnection
2 extends DataSource.Connection {
3 global SampleDataSourceConnection(DataSource.ConnectionParams
4 connectionParams) {
5 }
6// ...sync
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.
1// ...
2 override global List<DataSource.Table> sync() {
3 List<DataSource.Table> tables =
4 new List<DataSource.Table>();
5 List<DataSource.Column> columns;
6 columns = new List<DataSource.Column>();
7 columns.add(DataSource.Column.text('Title', 255));
8 columns.add(DataSource.Column.text('ExternalId', 255));
9 columns.add(DataSource.Column.number('Value', 15, 0));
10 columns.add(DataSource.Column.boolean('TrueOrFalse'));
11 columns.add(DataSource.Column.url('DisplayUrl'));
12 columns.add(DataSource.Column.text('Summary', 255));
13 tables.add(DataSource.Table.get('Sample', 'Title',
14 columns));
15 return tables;
16 }
17// ...query
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 DataSource.QueryContext is always only for a single table.
This sample custom adapter uses a helper method in the DataSource.QueryUtils class to filter and sort the results based on the WHERE and ORDER BY clauses in the SOQL query.
The DataSource.QueryUtils class and its helper methods can process query results locally within your Salesforce organization. This class is provided for your convenience to simplify the development of your Lightning Connect custom adapter for initial tests. However, the DataSource.QueryUtils class and its methods aren’t supported for use in production environments that use callouts to retrieve data from external systems. Complete the filtering and sorting on the external system before sending the query results to Salesforce. When possible, use server-driven paging or another technique to have the external system determine the appropriate data subsets according to the limit and offset clauses in the query.
1// ...
2 override global DataSource.TableResult query(
3 DataSource.QueryContext context) {
4 if (context.tableSelection.columnsSelected.size() == 1 &&
5 context.tableSelection.columnsSelected.get(0).aggregation ==
6 DataSource.QueryAggregation.COUNT) {
7 List<Map<String,Object>> rows = getRows(context);
8 List<Map<String,Object>> response =
9 DataSource.QueryUtils.filter(context, getRows(context));
10 List<Map<String, Object>> countResponse =
11 new List<Map<String, Object>>();
12 Map<String, Object> countRow =
13 new Map<String, Object>();
14 countRow.put(
15 context.tableSelection.columnsSelected.get(0).columnName,
16 response.size());
17 countResponse.add(countRow);
18 return DataSource.TableResult.get(context,
19 countResponse);
20 } else {
21 List<Map<String,Object>> filteredRows =
22 DataSource.QueryUtils.filter(context, getRows(context));
23 List<Map<String,Object>> sortedRows =
24 DataSource.QueryUtils.sort(context, filteredRows);
25 List<Map<String,Object>> limitedRows =
26 DataSource.QueryUtils.applyLimitAndOffset(context,
27 sortedRows);
28 return DataSource.TableResult.get(context, limitedRows);
29 }
30 }
31// ...search
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.SearchContext can have multiple tables selected. In this example, however, the custom adapter knows about only one table.
1// ...
2 override global List<DataSource.TableResult> search(
3 DataSource.SearchContext context) {
4 List<DataSource.TableResult> results =
5 new List<DataSource.TableResult>();
6 for (DataSource.TableSelection tableSelection :
7 context.tableSelections) {
8 results.add(DataSource.TableResult.get(tableSelection,
9 getRows(context)));
10 }
11 return results;
12 }
13// ...In the real world, you’d use Apex callouts to retrieve data from a remote Web service. For simplicity, however, this example generates the records in Apex.
1// ...
2 private List<Map<String,Object>> getRows(
3 DataSource.ReadContext context) {
4 List<Map<String, Object>> rows =
5 new List<Map<String, Object>>();
6 for (Integer i = 0; i < 50; i++) {
7 Map<String, Object> row = new Map<String, Object>();
8 String thisId = 'id' + i;
9 row.put('Title','FOO' + i);
10 row.put('Value', i);
11 row.put('DisplayUrl','http://www.test' + i + '.com');
12 row.put('TrueOrFalse', i < 30 ? true : false);
13 row.put('ExternalId', thisId);
14 rows.add(row);
15 }
16 return rows;
17 }
18}