You need to sign in to do that
Don't have an account?
kouidri nour
my contact merge batch generates an error too many soql queries
global class BatchMergeContact implements Database.Batchable<sObject>{
global BatchMergeContact(){
}
global Database.QueryLocator start(Database.BatchableContext BC){
List<AggregateResult> queryAggregate = new List<AggregateResult>();
List<String> querys = new List<String>();
List<Contact> cont = new List <Contact>();
queryAggregate = [select count(id), D_doublonnage_Email__c from contact group by D_doublonnage_Email__c having count(id)>1];
for(AggregateResult ag : queryAggregate){
querys.add((String)ag.get('D_doublonnage_Email__c'));
}
String query = 'select id, D_doublonnage_Email__c '+
'from Contact '+
'Where D_doublonnage_Email__c in :querys';
System.debug('KOUIDRI NOUR query : '+ query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<contact> scope){
Set<String> keyDedou = new Set<String>();
Map<String, List<contact>> ContactDouble = new Map<String, List<contact>>();
for(contact con :scope){
keyDedou.add(con.D_doublonnage_Email__c);
}
for(contact allCon : scope){
if(keyDedou.contains(allCon.D_doublonnage_Email__c)){
if(ContactDouble.keySet().contains(allCon.D_doublonnage_Email__c)){
ContactDouble.get(allCon.D_doublonnage_Email__c).add(allCon);
}
else{
ContactDouble.put(allCon.D_doublonnage_Email__c,new List<Contact>());
ContactDouble.get(allCon.D_doublonnage_Email__c).add(allCon);
}
}
}
Contact master = new Contact();
for(List<Contact> listContact : ContactDouble.values()){
if(listContact.size()==2){
Database.MergeResult results = Database.merge(listContact[0], listContact[1]);
}
else if (listContact.size()==3){
master=listContact.remove(0);
Database.MergeResult[] results = Database.merge(master, listContact);
}
}
}
global void finish(Database.BatchableContext BC){
List<AggregateResult> result = [SELECT D_doublonnage_Email__c, count(Id) FROM Contact GROUP BY D_doublonnage_Email__c Having count(Id)>1];
if(result.size()>0){
//Database.executeBatch(new BatchMergeContact());
}
}
}
global BatchMergeContact(){
}
global Database.QueryLocator start(Database.BatchableContext BC){
List<AggregateResult> queryAggregate = new List<AggregateResult>();
List<String> querys = new List<String>();
List<Contact> cont = new List <Contact>();
queryAggregate = [select count(id), D_doublonnage_Email__c from contact group by D_doublonnage_Email__c having count(id)>1];
for(AggregateResult ag : queryAggregate){
querys.add((String)ag.get('D_doublonnage_Email__c'));
}
String query = 'select id, D_doublonnage_Email__c '+
'from Contact '+
'Where D_doublonnage_Email__c in :querys';
System.debug('KOUIDRI NOUR query : '+ query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<contact> scope){
Set<String> keyDedou = new Set<String>();
Map<String, List<contact>> ContactDouble = new Map<String, List<contact>>();
for(contact con :scope){
keyDedou.add(con.D_doublonnage_Email__c);
}
for(contact allCon : scope){
if(keyDedou.contains(allCon.D_doublonnage_Email__c)){
if(ContactDouble.keySet().contains(allCon.D_doublonnage_Email__c)){
ContactDouble.get(allCon.D_doublonnage_Email__c).add(allCon);
}
else{
ContactDouble.put(allCon.D_doublonnage_Email__c,new List<Contact>());
ContactDouble.get(allCon.D_doublonnage_Email__c).add(allCon);
}
}
}
Contact master = new Contact();
for(List<Contact> listContact : ContactDouble.values()){
if(listContact.size()==2){
Database.MergeResult results = Database.merge(listContact[0], listContact[1]);
}
else if (listContact.size()==3){
master=listContact.remove(0);
Database.MergeResult[] results = Database.merge(master, listContact);
}
}
}
global void finish(Database.BatchableContext BC){
List<AggregateResult> result = [SELECT D_doublonnage_Email__c, count(Id) FROM Contact GROUP BY D_doublonnage_Email__c Having count(Id)>1];
if(result.size()>0){
//Database.executeBatch(new BatchMergeContact());
}
}
}
As you know this error appears when you exceed the Execution Governors Limit (you can run up to a total 100 SOQL queries in a single call or context).
The best way to troubleshoot this is by adding Limits.getQueries() in your code and capture debug logs to check how many queries are being consumed at any given line
System.debug('1.Number of Queries used in this apex code so far: ' + Limits.getQueries());
Salesforce cannot disable or raise the Governors Limit.
To fix the issue, change your code so that the number of SOQL fired is less than 100.
If you need to change the context, you can use @future annotation which will run the code asynchronously.
Best practices to avoid exceeding the Governors Limit:
Since Apex runs on a multi-tenant platform, the Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources.
- Avoid SOQL queries that are inside FOR loops.
- Follow the key coding principals for Apex Code in Developer Guide (https://developer.salesforce.com/page/Apex_Code_Best_Practices)
- Review best practices for Trigger and Bulk requests:
- Best practices for Triggers and Bulk requests (Force.com Apex Code Developer's Guide) (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm)
Reference:https://help.salesforce.com/articleView?id=000331875&type=1&mode=1If you find the above information helpful, please mark this as Solved by selecting the answer as best so that it can help others in the future. Thank You!
Anudeep