Callout class
Public class calloutValidation{
public static string CalloutCheck(String Email{
try{
//There are some logic
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setMethod('POST');
req.setEndpoint('callout:xxx');
req.setHeader('Content-Type', 'application/json');
req.setHeader('xxx', 'xxx');
req.setBody(reqBody);
System.debug(req);
System.debug(req.getEndpoint());
if (!Test.isRunningTest()) {
res = new http().send(req);
System.debug(res);
}
return res.getBody();
}
catch(CalloutException exp){
return exp.getMessage();
}
}
}
Batch class
public with sharing class xxxx implements Schedulable, Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return 'select id,Email from Contact Where Email != null';
}
public void execute(Database.BatchableContext bc, List<Contact> contactList){
// process each batch of records
EmailStatusValidationQueueable Queueable = new EmailStatusValidationQueueable(contactList);
System.enqueueJob(Queueable);
}
public void finish(Database.BatchableContext bc){
}
public with sharing class EmailStatusValidationQueueable implements Queueable,Database.AllowsCallouts {
public List<Contact> contactList;
public EmailStatusValidationQueueable(List<Contact> conList){
contactList = conList;
System.debug('contactList EmailStatusValidationQueueable: ' +contactList);
}
public void execute(QueueableContext context) {
// awesome code here
Integer sentCount = 0;
Boolean hitLimit = false;
List<Contact> updateContact = new List<Contact>();
for (Contact con : contactList) {
if (Limits.getCallouts() == Limits.getLimitCallouts()) {
hitLimit = true;
break;
}
// Do your callout
String res= calloutValidation.CalloutCheck(con.Email);
System.debug('res : ' +res);
if(res != null){
Map<String, Object> resultJsonData = (Map<String, Object>)JSON.deserializeUntyped(res);
System.debug('resultJsonData: ' +resultJsonData);
if(resultJsonData.get('message') != null && resultJsonData.get('message') == 'OK'){
con.xxxx = 'Validated';
System.debug('resultJsonData success');
updateContact.add(con);
System.debug('updateContact ' +updateContact);
}else{
System.debug('resultJsonData Fail');
}
}
// Track how many leads have been processed
sentCount++;
}
List<Database.SaveResult> result = Database.update(updateContact, false);
system.debug('result '+result);
for (Database.SaveResult r : result)
{
if (!r.isSuccess())
{
system.debug('not isSuccess ');
for (Database.Error e : r.getErrors())
{
system.debug('Error'+e);
}
}
}
if (hitLimit) {
// Need to chain this queueable to finish processing. First clean up
// the list of leads needing a callout, removing all those that have
// been sent already
for (Integer index = 0; index < sentCount; index++) {
contactList.remove(0);
}
// Now ensure the remainder get processed by simply re-enqueuing this
// queueable with its adjusted state
System.enqueueJob(this);
}
}
}
There is a batch class which will take all email in contact .(org have more than 4 lakh) Then from that batch class I need to pass each of email to this calloutValidation class and do the Api callout.(Because Post body support only one email at a time)
Can I do a for loop inside execute() method in batch and call the callout class by passing each contact email? Updated--->this only execute 7 records
callout inside for loop This is similar but I need support how can i apply that to my one.