I created a batch to find duplicate leads by email or phone or mobile phone.
The batch everytime finish with success, every single batch is processed. If I have in total 10 batches to processed, my success is 10. But I receive one failure too, with the message First error: line 1:220 mismatched character '<EOF>' expecting '''.
I already review my code but without success I was able to find what is causing this message.
I have a feeling that is have to do with emails like this email'email@gmail.com, but I used the escapeSingleQuotes to prevent injection.
My code is below.
Thank you
global class FZ_BatchDuplicateLeads Implements Database.batchable<sobject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id, FZ_ClientCode__c, FZ_SiteId__c, FZ_ClientEmail__c, FZ_MobilePhone__c, Phone FROM Account WHERE (RecordType.DeveloperName = \'FZ_PublicClient\' OR RecordType.DeveloperName = \'FZ_ProfessionalClient\') AND (FZ_ClientEmail__c != null OR FZ_MobilePhone__c != null OR Phone != null) ');
}
global void execute(Database.BatchableContext BC, List<Account> allAccounts){
for(Account account : allAccounts){
String whereFilter = '';
if(String.isNotBlank(account.FZ_ClientEmail__c)){
whereFilter += ' FZ_ClientEmail__c = \'' + String.escapeSingleQuotes(account.FZ_ClientEmail__c) + '\'';
}
if(String.isNotBlank(account.FZ_MobilePhone__c )){
if(whereFilter != ''){
whereFilter += ' OR ';
}
whereFilter += ' FZ_MobilePhone__c = \'' + account.FZ_MobilePhone__c + '\'';
}
if(String.isNotBlank(account.Phone)){
if(whereFilter != ''){
whereFilter += ' OR ';
}
whereFilter += ' Phone = \'' + account.Phone + '\'';
}
String query = 'SELECT Id, FZ_ClientCode__c, FZ_SiteId__c, FZ_ClientEmail__c, FZ_MobilePhone__c, Phone ' +
+ ' FROM Account ' +
+ ' WHERE RecordType.DeveloperName = \'FZ_Lead\' ' +
+ ' AND ( ' + whereFilter + ' ) ';
List<Account> leads = Database.query(query);
if(leads.size() > 0 && leads != null){
System.debug('account ' + account);
FZ_DuplicateLeads.dealWithDuplicateLeads(leads, account);
}
}
}
global void finish(Database.BatchableContext BC){
}
}
I solved my on problem; I need to escape the phone number variables because they could contains other characters along with the numbers.