I am pretty new to salesforce development. I am trying to make a Callout to the third party API whenever a Account record is created in Salesforce. I tried to make the future callout and didnt realize I need to bulkify the triggers so it threw Too many Future Callouts
when I tried to create more than 100 records through data loader. In the other question it was suggested to bulkify the triggers and check the callout limits so i changed my trigger to something like below
trigger createSegSubSegmentD365 on Account (after insert) {
Id AccParentRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Parent Account').getRecordTypeId();
List<String> jSONBody = new List<String>();
if(trigger.isinsert)
{
set<Id> AccountIds = new Set<Id>();
list<Account> acclist = new list<Account>();
for(Account acc : trigger.new){
AccountIds.add(acc.Id);
}
if(AccountIds.size()>0)
{
acclist = [Select id,Data_Source_ID__c,Parent.Data_Source_ID__c,RecordTypeId from Account WHERE Id IN: AccountIds ];
for(Account acc : acclist)
{
if(acc.RecordTypeId == AccParentRecordTypeId)
{
if(String.isNotBlank(acc.Data_Source_ID__c) ||String.isNotEmpty(acc.Data_Source_ID__c))
{
handleD365Request.SegJSON js = new handleD365Request.SegJSON();
js.dataAreaId = 'abc';
js.SegmentCode = acc.Data_Source_ID__c;
jSONBody.add(json.serialize(js));
}}}
String endPoint = '/data/ABCParentAccounts';
if(Limits.getFutureCalls() < Limits.getLimitFutureCalls()) {
// Calls the future Call method in the Class
handleD365Request.createSegSegment(jSONBody,endPoint);
}
else{
//not sure how to handle the call out the third party end points if the callout limit has reached
}
Currently my apex class is like below
@future (callout=true)
public static void createSegSegment(List<String> jsonBody,String endPoint) {
...............
for (String str : jsonBody)
{
Http http1 = new Http();
HttpRequest req1 = new HttpRequest();
String d365EndPoint = resource + endPoint;
req1.setMethod('POST');
req1.setTimeout(20000);
req1.setHeader('Authorization','Bearer '+bearerToken);
req1.setEndpoint(d365EndPoint);
req1.setBody(str);
req1.setHeader('Content-Type', 'application/json;charset=UTF-8');
HttpResponse res1 = http1.send(req1);
}
}
Should I be calling the queueable if the future Callout limit has reached? Or should I be moving my entire logic to queueable as it offers more advantage than the Batch/future. Can anyone suggest how can I be using the queueable here. Appreciate all your help.