---
schema_version: "1"
id: 9559e86ee1b77c439315da78004405cfaaefc07f17ed2e92e0e1d86be7f02125
title: Aggregation for Salesforce Connect Custom Adapters
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_connector_aggregation.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connector_aggregation.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: d27511bb207f3db496984f0e4476e38e42934ed9deb7adf6b695e22c904abf17
description: 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.
---
# 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.

// Handle COUNT() queries
if (context.tableSelection.columnsSelected.size() == 1 &&      
    context.tableSelection.columnsSelected.get(0).aggregation == 
        QueryAggregation.COUNT) {
    List<Map<String, Object\>> countResponse = new List<Map<String, Object\>>();
    Map<String, Object\> countRow = new Map<String, Object\>();
    countRow.put(context.tableSelection.columnsSelected.get(0).columnName, 
    response.size());
    countResponse.add(countRow);
    return countResponse;
}

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.

override global DataSource.TableResult query(DataSource.QueryContext context) {
    List<Map<String,Object\>> rows = retrieveData(context);
    List<Map<String,Object\>> response = postFilterRecords(
            context.tableSelection.filter, rows);
    if (context.tableSelection.columnsSelected.size() == 1 &&        
        context.tableSelection.columnsSelected.get(0).aggregation ==   
                DataSource.QueryAggregation.COUNT) {
        List<Map<String, Object\>> countResponse = new List<Map<String, 
                Object\>>();
        Map<String, Object\> countRow = new Map<String, Object\>();
        countRow.put(context.tableSelection.columnsSelected.get(0).columnName, 
                response.size());
        countResponse.add(countRow);
        return DataSource.TableResult.get(context, countResponse);
    }
    return DataSource.TableResult.get(context, response);
}

#### See Also

- [_Apex Reference Guide_: QueryContext Class](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_class_DataSource_QueryContext.htm "Apex Reference Guide: QueryContext Class - HTML (New Window)")
- [Create a Sample DataSource.Connection Class](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connector_start_connection_class.htm "First, create a DataSource.Connection class to enable Salesforce to obtain the external system’s schema and to handle queries and searches of the external data.")