Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JitendraZaa/3d13b45a8524bad4672e1cb2d78f3785 to your computer and use it in GitHub Desktop.
Save JitendraZaa/3d13b45a8524bad4672e1cb2d78f3785 to your computer and use it in GitHub Desktop.
Salesforce Connect - Apex Adapter - query method.java
/**
* This method is called everytime when SOQL is issued against external Object
* or while using list view or viewing detail page.
*
* Note : Ideally searching should be done at server side however for sake of
* simplicity, we would be using "DataSource.QueryUtils" class provided by
* Salesforce. In this case, filtering and sorting would be done in Salesforce
* once response returned by external REST API
* */
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( );
List<Map<String,Object>> response = DataSource.QueryUtils.filter(context, getRows( ));
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( ));
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);
}
}
/**
* Make REST callout to external system and parse the result in required format
* */
private List<Map<String,Object>> getRows( ){
List<Map<String, Object>> lstReturnDataset = new List<Map<String, Object>>();
HttpRequest req = new HttpRequest();
req.setEndPoint(EXTERNAL_SRC_URL);
req.setMethod('GET');
HttpResponse res = new Http().send(req);
Map<String, Object> json_resp = ( Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> lstBlog = (List<Object>)json_resp.get('blog');
for(Object blog : lstBlog){
Map<String, Object> blogMap = (Map<String, Object>) blog;
Map<String, Object> curRow = new Map<String, Object>();
curRow.put(COL_HEADER_TITLE,blogMap.get(COL_HEADER_TITLE));
curRow.put(COL_HEADER_SEO_KEYWORDS,blogMap.get(COL_HEADER_SEO_KEYWORDS));
curRow.put(COL_HEADER_EXCERPT,blogMap.get(COL_HEADER_EXCERPT));
curRow.put(COL_HEADER_CONTENT,blogMap.get(COL_HEADER_CONTENT));
curRow.put(COL_HEADER_LANGUAGE,blogMap.get(COL_HEADER_LANGUAGE));
curRow.put(COL_HEADER_DISPLAYURL,EXTERNAL_SRC_BASEURL+blogMap.get(COL_HEADER_BLOGLINK));
curRow.put(COL_HEADER_EXTERNAL_ID, String.valueOf(blogMap.get(COL_HEADER_BLOGLINK)).replaceAll('/','_'));
lstReturnDataset.add(curRow);
}
return lstReturnDataset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment