+ Start a Discussion
Andre MurilloAndre Murillo 

Get record ID from a DataSourceConnection class

Hi!

I have a Custom DataSourceConnection class (that extends DataSource.Connection) to retrieve related records for contacts from my server API. I need the record ID for the current contact to make my query, but haven't found a way to retrieve it (ApexPages won't work because it's not using Visualforce).

How can I get the record ID from my DataSourceConnection class?

Thanks for the help!
 
Andre MurilloAndre Murillo
Here are more details on my issue:

The class is being used/called by an External Object that uses a External Data Source, which uses my Custom DataSourceProvider + DataSourceConnection. It's based on this article:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connector_start.htm

I need to get the record ID in the "SampleDataSourceConnection" class, inside the "getRows" method that is called by the overrided "query" method. This is how the code looks:
 
global class SampleDataSourceConnection
    extends DataSource.Connection {
    global SampleDataSourceConnection(DataSource.ConnectionParams
        connectionParams) {
    }
// ...

// ...
    override global DataSource.TableResult query(
        DataSource.QueryContext context) {
        if (context.tableSelection.columnsSelected.size() == 1 &&
            context.tableSelection.columnsSelected.get(0).aggregation ==
                DataSource.QueryAggregation.COUNT) {
                List<Map<String,Object>> rows = getRows(context);
                List<Map<String,Object>> response =
                    DataSource.QueryUtils.filter(context, getRows(context));
                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);
        } else {
            List<Map<String,Object>> filteredRows =
                DataSource.QueryUtils.filter(context, getRows(context));
            List<Map<String,Object>> sortedRows =
                DataSource.QueryUtils.sort(context, filteredRows);
            List<Map<String,Object>> limitedRows =
                DataSource.QueryUtils.applyLimitAndOffset(context,
                    sortedRows);
            return DataSource.TableResult.get(context, limitedRows);
        }
    }
// ...

// ...
    // Helper method to get record values from the external system for the Sample table.
    private List<Map<String, Object>> getRows () {
        // Get row field values for the Sample table from the external system via a callout.
        HttpResponse response = makeGetCallout();
        // Parse the JSON response and populate the rows.
        Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(
                response.getBody());
        Map<String, Object> error = (Map<String, Object>)m.get('error');
        if (error != null) {
            throwException(string.valueOf(error.get('message')));
        }
        List<Map<String,Object>> rows = new List<Map<String,Object>>();
        List<Object> jsonRows = (List<Object>)m.get('value');
        if (jsonRows == null) {
            rows.add(foundRow(m));
        } else {
            for (Object jsonRow : jsonRows) {
                Map<String,Object> row = (Map<String,Object>)jsonRow;
                rows.add(foundRow(row));
            }
        }
        return rows;
    }
// ...

I understand that the External Object relates to the Contact Object automatically by the ContactId, but I don't have the ContactId on the external system, so I have to grab the contact email (and so the current contact id) to relate it with the external system...
helin jessihelin jessi
I hope you like my post and get an appropriate direction to take the JCPenney Survey at
https://customerfeedbacks.xyz/jcpenney-survey-talktojcpenney/
Tyson MillerTyson Miller
I am very thankful that you provided this information to us TalkToWendys (https://www.talktowendys.org/).
math mrnmath mrn
All the customers who successfully fill up the Survey form, one want to know the result of the survey. Winner of Wendy’s Survey can get 500$ in cash.https://talktowendysurvey.com