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
Shruthi NarsiShruthi Narsi 

Error Apex Code

I want to delete dupicate accounts below code for your reference. Attached is the error
global class DuplicateRecords implements Database.Batchable<SObject>  {

     

  Global Map<String , Account__c> AccountNumberBookmap = new Map<String ,  Account__c>();

     

    global Database.QueryLocator start(Database.BatchableContext BC){

      return Database.getQueryLocator([Select AccountNumber__c from Account__c where AccountNumber__c != null]);

      }

     

     

     

    global void excute(Database.BatchableContext BC , List<AccountNumber__c> scope){

         

      

         

         

      // Map<String , AccountNumber__c> AccountBookmap = new Map<String , AccountNumber__c>();

         

        List<AccountNumber__c> duplicatelist = new List<AccountNumber__c>();

        for(AccountNumber__c s : scope){

            if(! AccountBookmap.containsKey(s.Account__c)){

                AccountBookmap.put(s.Account__c , s);

            }

            else{

                duplicatelist.add(s);          

            }                       

        } 

                

        

       system.debug(duplicatelist);

        if(duplicatelist.size() > 0){

            delete duplicatelist;

        }

    }

     

    global void finish(Database.BatchableContext BC){

         

    }

         

 

}
User-added image
Ajay K DubediAjay K Dubedi
Hi Shruthi Narsi,

You can remove the error by using below code. I have tested it in my org. It is working fine.
'I am assuming that you have made a custom Account__c in your org.' if you are working on standard Account object, make sure to remove '__c' from Account and AccountNumber in the code.
  
global class DuplicateRecords implements Database.Batchable<sObject>  {
  global Map<Account__c , String> AccountBookmap = new Map<Account__c ,  String>();
   global Database.QueryLocator start(Database.BatchableContext BC){

      return Database.getQueryLocator([Select AccountNumber__c from Account__c where AccountNumber__c != null]);

      }
    global void execute(Database.BatchableContext BC, List<Account__c> scope){
        List<Account__c> duplicatelist = new List<Account__c>();
        for(Account__c s : scope){
            if(! AccountBookmap.containsKey(s)){
                AccountBookmap.put(s , s.AccountNumber__c);
            }

            else{
                duplicatelist.add(s);          
            }                       
        } 
       system.debug(duplicatelist);
        if(duplicatelist.size() > 0){
            delete duplicatelist;
        }
    }
    global void finish(Database.BatchableContext BC){

        System.debug('Duplicate Accounts has been deleted successfully');
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
devedeve
Hi Shruthi,

You have not implemented execute method in this batch class. Its showing error  in problems section in your screenshot. Please correct spelling of execute in your code.

Thanks
ManojjenaManojjena
HI ,

Please check below code . It will help . 

I have added one extra line while we need to check the map empty else it will always add the first account to the duplicate list .
Also as we are doing the delete operation in finish method we need to have state ful to maintain the state of the  map .
 
global class DuplicateRecords implements Database.Batchable<sObject>,Database.Stateful{
  global Map<Account__c,String> AccountBookmap= new Map<Account__c,String>();
   global List<Account__c> duplicatelist = new List<Account__c>();
   global Database.QueryLocator start(Database.BatchableContext BC){

      return Database.getQueryLocator([Select AccountNumber__c  from Account__c where AccountNumber__c  != null]);

      }
    global void execute(Database.BatchableContext BC, List<Account__c> scope){
        
        for(Account acc : scope){
            if(!AccountBookmap.isEmpty()){
                if(! AccountBookmap.containsKey(acc)){
                   AccountBookmap.put(acc,acc.AccountNumber__c );
                }
             else{
                duplicatelist.add(acc);          
            }  
              AccountBookmap.put(acc,acc.Name);  
           }
        } 
       system.debug(duplicatelist);
        
    }
    global void finish(Database.BatchableContext BC){
         if(duplicatelist.size() > 0){
            delete duplicatelist; 
         }
        System.debug('Duplicate Accounts has been deleted successfully');
    }

}

Check incase workin let me know .
Thanks 
Manoj