function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhilip DussaDhilip Dussa 

Help me on Batch Merge

Good Morning,

I facing an error in Batch class : 

Merge failed. First exception on row 0 with id 0030U00001ESVzyQAH; first error: LIMIT_EXCEEDED, System.LimitException: Too many queueable jobs added to the queue: 2: []

Code: 

global class MergeContactBatch implements database.Batchable<sObject>{
    
    String Name = '%Test%';
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id FROM Contact WHERE Email = NULL AND Name LIKE \''+Name+'\'';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        
        Contact masterConact = MergeContactBatch.getContact();
        
        List<Contact> dupcontactList = new List<Contact>();
        Set<Id> Ids = new Set<Id>();
        for(Contact contactRecrd : scope){
            Ids.add(contactRecrd.Id);
            dupcontactList.add(contactRecrd);
        }
        
        List<Contact> contactList = [SELECT Id FROM Contact WHERE Id IN: Ids];
        for(Contact con : contactList){
            Database.merge(masterConact, con);
        }
        
       // Database.MergeResult[] results = Database.merge(masterConact, dupcontactList);
    }
    
    global void finish(Database.BatchableContext BC){}
    
    
    public Static Contact getContact(){
        
        List<Contact> contactList = [SELECT Id FROM Contact WHERE Id = '0030U00001ESW0S'];
        if(!contactList.isEmpty()){
            return contactList[0];
        }else{
            return null;
        }
    }
}

please help me,

Thanks,
Dhilip Dussa
Shatrughna SalunkeShatrughna Salunke
Hi Dhilip,

Please try with below code it is working fine for me. please do let me know still if it is not working

global class MergeContactBatch implements database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        
        String name  = '%Test%';
        String query = 'SELECT Id, Name FROM Contact WHERE Name like \''+name+'\'';
        return Database.getquerylocator(query);    
    }
    
    global void execute (Database.BatchableContext BC, List<Contact> scope) {
          
         Contact masterConact = MergeContactBatch.getContact();
         List<Contact> dupcontactList = new List<Contact>();
         Set<Id> Ids = new Set<Id>();
          
         for(Contact contactRecrd : scope) {
                Ids.add(contactRecrd.Id);
                dupcontactList.add(contactRecrd);
          }
          
          List<Contact> contactList = [SELECT Id FROM Contact 
                                                    WHERE Id IN: Ids AND id!= '0032v00003lPbkLAAS'];
          
          Database.MergeResult[] results = Database.merge(masterConact, contactList, true);
 }     
 
  global void finish (Database.BatchableContext BC) {} 
    public static Contact getContact(){
        
        // Query for  masterRecord  
        List<Contact> contactList = [SELECT Id FROM Contact WHERE Id = '0032v00003lPbkLAAS'];
        if(!contactList.isEmpty()){
            return contactList[0];
        }else{
            return null;
        }
    }
 }

Regards,
Shatrughna