Retrieve Data Using Apex Code with Viewer
Overview: use Apex (Callable interface) to return JSON data to Viewer templates.
Basic steps:
- Create an Apex class implementing Callable.
- Return a valid JSON object.
- Update "External Data Class Name" in your template to the Apex API name.
Best practices: follow Salesforce Apex limits, write tests, and keep code lean.

Example Apex (simplified)
GetDataPluginExt.cls
global with sharing class GetDataPluginExt implements Callable {
public Object call(String action, Map<String, Object> args) {
Map<String, Object> input = (Map<String, Object>) args.get('input');
Map<String, Object> output = (Map<String, Object>) args.get('output');
Map<String, Object> options = (Map<String, Object>) args.get('options');
return invokeMethod(action, input, output, options);
}
private Object invokeMethod(String methodName, Map<String, Object> inputMap, Map<String, Object> outMap, Map<String, Object> options) {
switch on methodName {
when 'getData' {
String APIUrl = 'https://dummyjson.com/products/' + String.valueOf(Integer.valueOf(Math.round(Math.random()*30)));
HttpRequest req = new HttpRequest();
req.setEndpoint(APIUrl);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
req.setTimeout(120000);
HTTPResponse res = new Http().send(req);
Map<String, Object> objectsList = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
if (objectsList == null) return null;
return objectsList;
}
when else {
return '';
}
}
}
}
You must return a valid JSON-serializable object that Viewer can consume.