Requirement is to loop through set of records(could be in 1000's) and make a REST callout to process those records. I did went through multiple Q&A posted in the community but still stuck with it. I did tried to approach the solution per @Jeremy Nottingham on this post but not successful yet. Can we callout and chain a Queueable class?
public class Log_Processor {
public class FlowInputs{
@InvocableVariable public List<Log__c> inputLog;
@InvocableVariable public List<Log_Processor__mdt> logMetada;
@InvocableVariable public String ProcessorType;
}
@InvocableMethod(label='Process Exception')
public static void processLog(List<FlowInputs>logList){
List<Log__c> logs = logList[0].inputLog;
List<Log_Processor__mdt> logMeta = logList[0].logMetada;
if(logList[0].ProcessorType == 'Bulk'){
ID jobID = System.enqueueJob(new PDS_Batch_Log_Processor(logs,logMeta));
}
}
}
public class Batch_Log_Processor implements Queueable, Database.AllowsCallouts{
public List<Log__c> logs;
public List<Log_Processor__mdt> logMeta;
public Batch_Log_Processor(List<Log__c>logList,List<Log_Processor__mdt>logMetaList){
this.logs = logList;
this.logMeta = logMetaList;
}
public Batch_Log_Processor(){
}
public void execute(QueueableContext context) {
for(Integer i=0; i< logs.size(); i++){
String extValue = logs[i].External_ID_Value__c;
if(String.isNotBlank(extValue)){
String extValue1= extValue.deleteWhitespace();
processPayload(logMeta[0].End_Point__c+extValue1,
logMeta[0].Method__c,
logs[i].Message__c,
logs[i].Id
);
logs.remove(i);
}
}
}
@future(callout=true)
public static void processPayload(String reqURL,String reqMethod,
String reqBody,String logID){
HttpRequest httpRequest = new HttpRequest();
httpRequest.setMethod(reqMethod);
httpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
httpRequest.setHeader('Content-Type','application/json');
httpRequest.setEndpoint(reqURL);
httpRequest.setBody(reqBody);
Log__c logToUpdate = new Log__c();
logToUpdate.Id = logID;
logToUpdate.Processed_Date__c = System.now();
try {
Http http = new Http();
HttpResponse httpResponse = http.send(httpRequest);
logToUpdate.Stack_Trace__c = httpResponse.getBody();
} catch( System.Exception e) {
logToUpdate.Stack_Trace__c = String.valueOf(e);
System.debug('ERROR: '+ e);
}
//update logToUpdate;
System.enqueueJob(new PDS_Batch_Log_Processor());
}
I still get the error "Too many future calls:51". I am not sure if I misunderstood the post or I am not the doing the chaining properly.
@sfdcfox: Thanks for the response. I didn't follow this line. Log object doesn't have endpoint, requestMethod as those are coming from custom metadata object. I am passing all the input parameters as string to processpayload method. logsToUpdate.add(processPayload(logMeta[0].End_Point__c+extValue1, logMeta[0].Method__c, nextLog.Message__c, nextLog.Id)); – user28452 Nov 02 '21 at 03:30@sfdcfox: Thank you again. I am upvoting as this helped me to move in the right direction. – user28452 Nov 02 '21 at 21:59