Salesforce Apex External Data Sources – Yelp API

Sample code to create Yelp Bussiness API as an external data source and create an external object. Create  DataSource Connection class with below code.

global class YelpConnection extends DataSource.Connection{
    
    private DataSource.ConnectionParams connectionInfo;
    global YelpConnection(DataSource.ConnectionParams connectionInfo) {
        this.connectionInfo = connectionInfo;
    }
    override global List<DataSource.Table> sync() {
        List<DataSource.Table> tables = new List<DataSource.Table>();
        List<DataSource.Column> userColumns = new List<DataSource.Column>();
        userColumns.add(DataSource.Column.text('name', 255));
        userColumns.add(DataSource.Column.text('image_url', 255));
        userColumns.add(DataSource.Column.text('isClosed',255));
        userColumns.add(DataSource.Column.text('reviewcount',255));
        userColumns.add(DataSource.Column.url('DisplayUrl'));
        userColumns.add(DataSource.Column.text('ExternalId',255));
        tables.add(DataSource.Table.get('Yelp Table','title', userColumns));
        return tables;
        
    }
    override global DataSource.TableResult query(DataSource.QueryContext context) {
        String url = 'https://api.yelp.com/v3/businesses/search?location=CA';
        List<Map<String, Object>> rows =
            DataSource.QueryUtils.process(context, getData(url));
        return DataSource.TableResult.get(true, null, context.tableSelection.tableSelected, rows);
    }
    
    public List<Map<String, Object>> getData(String url) {
        String response = getResponse(url);
        List<Map<String, Object>> rows =
            new List<Map<String, Object>>();
        Map<String, Object> responseBodyMap = (Map<String, Object>)JSON.deserializeUntyped(response);
        List<Object> fileItems=(List<Object>)responseBodyMap.get('businesses');
        if (fileItems != null) {
            for (Integer i=0; i < fileItems.size(); i++) {
                Map<String, Object> item = 
                    (Map<String, Object>)fileItems[i];
                rows.add(createRow(item));  
            }
        } else {
            rows.add(createRow(responseBodyMap));
        }
        
        return rows;
    }
    
    
    
    public Map<String, Object> createRow(Map<String, Object> item){
        
        Map<String, Object> row = new Map<String, Object>();
        for ( String key : item.keySet() ) {
            if (key == 'id') {
                row.put('ExternalId', item.get(key));
            } else if (key=='url') {
                row.put('DisplayUrl', item.get(key));
                row.put('image_url', item.get(key));
            } 
            else if (key=='name') {
                row.put('name', item.get(key));
            }else if (key=='is_closed') {
                row.put('isClosed', item.get(key));
            }else if (key=='review_count') {
                row.put('reviewcount', item.get(key));
            }else {
                row.put(key, item.get(key));
            }
        }
        return row;   
    }
    public String getResponse(String url) {
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint(url);
        request.setMethod('GET');
        request.setHeader('Authorization', 'Bearer RCbmuhqeCKSvQkx3iXmqVBVh4hvjBjCYFMT_f1v4ZCHb2bVnBsr-wm-gNIRpHKm6IOyxBtJWmjpBm-5gR8j7_qNZVFHBEM3cg4-GienUZQTMOesjO6Brm2WZotvWnYx');
        HttpResponse response = h.send(request);
        return response.getBody();
        
    } 
    
}

Create Datasource Provide Class with below code

global class YelpDataSourceProvide extends DataSource.Provider{
    override global List<DataSource.AuthenticationCapability> getAuthenticationCapabilities() {
        List<DataSource.AuthenticationCapability> capabilities = new List<DataSource.AuthenticationCapability>();
        capabilities.add(DataSource.AuthenticationCapability.ANONYMOUS);
        capabilities.add(DataSource.AuthenticationCapability.BASIC);
        return capabilities;
    }
    override global List<DataSource.Capability> getCapabilities() {
        List<DataSource.Capability> capabilities =  new List<DataSource.Capability>();
        capabilities.add(DataSource.Capability.ROW_QUERY);
                capabilities.add(DataSource.Capability.SEARCH);

        return capabilities;
    }
    override global DataSource.Connection getConnection(DataSource.ConnectionParams connectionParams) {
        return new YelpConnection(connectionParams);
    }
    
}

Now create new External data source as shown below.

Click save and after that click validate and Sync to create an external object

Create a tab which will display the data as shown below.