I have a requirement in which, for each time an account is inserted, I have to make a callout to an external system sending some data of the record and then the API returns a unique Id that I have to assign to a field in Account, and then update it. (basically the account is saved in the external system, syncing the accounts in the org and in the external system)
So the flow would be: Account inserted => trigger after insert => callout => update on account.
This callout also has to be made if certain fields are updated on account, so the trigger would also be defined for after update, and if the criteria is matched, the callout is also made. This flow would be Account updated => if criteria matched => callout
I have to design the code so as to handle many records inserted at once. I know triggers require callouts to be made asynchronously. This is where I'm blocked as to what approach is best.
The limitations I have to consider is the max 100 callouts per transaction and the fact I cannot call future methods from a batch class so that won't be possible.
I already have a class with methods that receive an account as parameter and makes the callout:
public static Account altaCF(Account cuenta){
String body = JSON.serialize(new Consumidor(cuenta));
TopGroup_Integration__c cred = TopGroup_Integration__c.getOrgDefaults();
HttpResponse response = doRequest(body, cred.Endpoint_CF__c, cred.Api_Username__c, cred.Api_Token__c);
handleResponse(response, cuenta);
return cuenta;
}
doRequest method:
public static HttpResponse doRequest(String requestBody, String endpoint, String username, String token) {
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('authorization-type', 'Basic');
request.setHeader('api-username', API_USERNAME);
request.setHeader('api-token', API_TOKEN);
request.setBody(requestBody);
HttpResponse response = new Http().send(request);
return response;
}
This is the handleResponse method:
public static void handleResponse(HttpResponse res, Account cuenta){
if(res.getStatusCode() == 200){
SuccessResponse responseBody = (SuccessResponse) JSON.deserialize(JSON.serialize(res.getBody()), SuccessResponse.class);
cuenta.ID_Top_Group__c = responseBody.data.numeroCliente;
cuenta.TG_Service_Status__c = responseBody.statusCode;
cuenta.TG_Service_Message__c = responseBody.data.message;
}else{
FailResponse responseBody = (FailResponse) JSON.deserialize(JSON.serialize(res.getBody()), FailResponse.class);
cuenta.TG_Service_Status__c = responseBody.status;
cuenta.TG_Service_Message__c = generateServiceErrorMessage(responseBody);
}
}
My idea was to call this the altaCF method from my Trigger Handler class, for each record inserted.
Now what I have to figure out is: 1)the best approach to make the logic asynchronous (don't know if a future method or batch or queuable is best. 2)how to handle large number of records inserted at once.
I know triggers execute in batches of 200, and since the limit of callouts per transaction is 100 I have to consider that. The ideal scenario would be to handle this in a batch class but I don't know if that is enough to meet the "asynchronous callout" requirement of triggers.
Any help would be appreciated!