Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Aggregation for Salesforce Connect Custom Adapters

If you receive a COUNT() query, the selected column has the value QueryAggregation.COUNT in its aggregation property. The selected column is provided in the columnsSelected property on the tableSelection for the DataSource.QueryContext.

The following example illustrates how to apply the value of the aggregation property to handle COUNT() queries.

1// Handle COUNT() queries
2if (context.tableSelection.columnsSelected.size() == 1 &&      
3    context.tableSelection.columnsSelected.get(0).aggregation == 
4        QueryAggregation.COUNT) {
5    List<Map<String, Object>> countResponse = new List<Map<String, Object>>();
6    Map<String, Object> countRow = new Map<String, Object>();
7    countRow.put(context.tableSelection.columnsSelected.get(0).columnName, 
8    response.size());
9    countResponse.add(countRow);
10    return countResponse;
11}

An aggregate query can still have filters, so your query method can be implemented like the following example to support basic aggregation queries, with or without filters.

1override global DataSource.TableResult query(DataSource.QueryContext context) {
2    List<Map<String,Object>> rows = retrieveData(context);
3    List<Map<String,Object>> response = postFilterRecords(
4            context.tableSelection.filter, rows);
5    if (context.tableSelection.columnsSelected.size() == 1 &&        
6        context.tableSelection.columnsSelected.get(0).aggregation ==   
7                DataSource.QueryAggregation.COUNT) {
8        List<Map<String, Object>> countResponse = new List<Map<String, 
9                Object>>();
10        Map<String, Object> countRow = new Map<String, Object>();
11        countRow.put(context.tableSelection.columnsSelected.get(0).columnName, 
12                response.size());
13        countResponse.add(countRow);
14        return DataSource.TableResult.get(context, countResponse);
15    }
16    return DataSource.TableResult.get(context, response);
17}