APEX
An example Apex code for calling Dynamo batch API.
The code gets max two (2) account IDs and initiates batch API post using record IDs as parameters.
Note: Please fill in templateID and apiKey variables.
CODE
global class DynamoBatchApiExample {
public static void sendRequest()
{
Account[] records = [SELECT Id FROM Account LIMIT 2];
List<String> sObjectIds = new List<String>();
for (SObject r : records) {
String id = (String) r.get('Id');
sObjectIds.add(id);
}
String templateID = 'XXXX';
String apiKey = 'YYYY';
String bodyJson = buildBody(templateID,sObjectIds);
String resultJson = sendRequest(bodyJson,apiKey,null);
System.debug(resultJson);
}
public static String sendRequest(String bodyJson,String apiKey,String endPoint)
{
HttpRequest req = new HttpRequest();
if (String.isBlank(endPoint))
endPoint = 'https://dynamo-api.documill.com/v1/batch';
req.setEndpoint(endPoint);
req.setMethod('POST');
req.setTimeout(120000);
req.setHeader('x-dynamo-tenant-id', UserInfo.getOrganizationId());
req.setHeader('x-dynamo-api-key', apiKey);
req.setHeader('Content-Type', 'application/json');
req.setBody(bodyJson);
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
}
private static String buildBody(String templateID, List<String> sObjectIds)
{
String partnerServerURL = URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/u/50.0/' + UserInfo.getOrganizationId();
List<String> l = new List<String>();
l.add('{');
l.add('"template" : "' + templateID + '",');
l.add('"tasks" : [');
Integer i = 0;
for (String sid : sObjectIds) {
l.add('{');
l.add('"params" : {');
l.add('"id" : "' + sid + '"');
l.add('}');
l.add('}');
if(++i < sObjectIds.size()){
l.add(',');
}
}
l.add('],');
l.add('"integration" : {');
l.add('"type" : "Salesforce",');
l.add('"sessionID" : "' + UserInfo.getSessionId() + '",');
l.add('"serverURL" : "' + partnerServerURL + '"');
l.add('}');
l.add('}');
String body = String.join(l,'');
return body;
}
}
The Apex class can be started from developer console: Debug -> Open Execute Anonymous Window -> DynamoBatchApiExample.sendRequest();