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
Yoni LegacyYoni Legacy 

Help in converting my apex class to batch job

Hi Experts,

I hope someone can help to convert my class to batch job. This is the first time that I will create a batch job and only a newbie in apex coding.

Here is my class which is working but hitting the DML limit as there are more than 10k to insert.
 
public with sharing class Eisai_AccountListCreation_New {
    
    List<Account_Territory_Mapping__c> allAtmRecs = new List<Account_Territory_Mapping__c>();
    List<Account_List_vod__c> ToInsertAcctList = new List<Account_List_vod__c>();
        
    public Eisai_AccountListCreation_New(){
        
        allAtmRecs = [Select Account_Name_String__c, Account_Owner__c FROM Account_Territory_Mapping__c 
                      												  WHERE Owner_Territory__c LIKE 'EP%' 
                     												  OR Owner_Territory__c LIKE 'QM%'];
        
        for(Account_Territory_Mapping__c  Atm : allAtmRecs){
            if(Atm.Account_Name_String__c != Null && Atm.Account_Owner__c !=Null){
                    Account_List_vod__c AccList = new Account_List_vod__c();
                        AccList.Name = 'HO_' + Atm.Account_Name_String__c;
                        AccList.OwnerId = Atm.Account_Owner__c;
                        ToInsertAcctList.add(AccList);
            }   
        }
        
      System.debug('No of Account List to insert: ' + ToInsertAcctList.size());
        
      Database.insert(ToInsertAcctList,false);

      /*  
      for (Database.SaveResult sr: srList){
            if(sr.isSuccess()){
                System.debug('Inserted Account List count: ' + sr.getId());
            } else {
                for(Database.Error err : sr.getErrors()){
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                }
            }
       } */
        
    }//End of Constructor

}//End of Class

Below is the batch class which I tried to create but I encountered an error.
 
global class Eisai_BatchInsertAcctList implements Database.batchable<sObject>, Database.Stateful { 
    
    global string query = 'Select Account_Name_String__c, Account_Owner__c FROM Account_Territory_Mapping__c
        															  	   WHERE Account_Name_String__c != Null 
                     												       AND Account_Owner__c !=Null;
                      												  
	global Iterable<sObject> start(Database.BatchableContext info){
        return Database.getQueryLocator(query);
    }
    
	global void execute(Database.BatchableContext info, List<Account_Territory_Mapping__c> scope){
        List<Account_List_vod__c> ToInsertAcctList = new List<Account_List_vod__c>();
        
        for(Account_Territory_Mapping__c  Atm : scope){
            if(Atm.Account_Name_String__c != Null && Atm.Account_Owner__c !=Null){
                    Account_List_vod__c AccList = new Account_List_vod__c();
                        AccList.Name = 'HO_' + Atm.Account_Name_String__c;
                        AccList.OwnerId = Atm.Account_Owner__c;
                        ToInsertAcctList.add(AccList);
            }   
        }
        
    Database.insert(ToInsertAcctList,false);
        
	}
	global void finish(Database.BatchableContext info){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'marion.c.d.legacion@accenture.com'};
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('Eisai_BatchInsertAcctList Batch Job Run Complete');
            
         System.debug('Insert Account List Count: ' + ToInsertAcctList.size());
           
    }
        
}//end of batch class

Below is the error messasge which I encountered:
User-added image

Thanks,
Marion
Best Answer chosen by Yoni Legacy
BALAJI CHBALAJI CH
Hi Yoni Legacy,

The error "line breaks not allowed in string literals" says that the string is not closed/opened properly.
Please find below modified code of Batch class.
 
global class Eisai_BatchInsertAcctList implements Database.batchable<sObject>, Database.Stateful
{
    global string query = 'Select Account_Name_String__c, Account_Owner__c FROM Account_Territory_Mapping__c WHERE Account_Name_String__c != Null AND Account_Owner__c !=Null';
    
    global Iterable<sObject> start(Database.BatchableContext info){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext info, List<Account_Territory_Mapping__c> scope){
        List<Account_List_vod__c> ToInsertAcctList = new List<Account_List_vod__c>();
        
        for(Account_Territory_Mapping__c  Atm : scope){
            if(Atm.Account_Name_String__c != Null && Atm.Account_Owner__c !=Null)
            {
                Account_List_vod__c AccList = new Account_List_vod__c();
                AccList.Name = 'HO_' + Atm.Account_Name_String__c;
                AccList.OwnerId = Atm.Account_Owner__c;
                ToInsertAcctList.add(AccList);
            }   
        }
        
        insert ToInsertAcctList;  
    }
    
    global void finish(Database.BatchableContext info){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        list<string> toAddresses = new list<string>();
        toAddresses.add('marion.c.d.legacion@accenture.com');
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('Eisai_BatchInsertAcctList Batch Job Run Complete');
        
        System.debug('Insert Account List Count: ' + ToInsertAcctList.size());
        
    }
    
}//end of batch class

Let me know if that works for you.

Best Regards,
BALAJI

All Answers

Yoni LegacyYoni Legacy
There error is in row 3
BALAJI CHBALAJI CH
Hi Yoni Legacy,

The error "line breaks not allowed in string literals" says that the string is not closed/opened properly.
Please find below modified code of Batch class.
 
global class Eisai_BatchInsertAcctList implements Database.batchable<sObject>, Database.Stateful
{
    global string query = 'Select Account_Name_String__c, Account_Owner__c FROM Account_Territory_Mapping__c WHERE Account_Name_String__c != Null AND Account_Owner__c !=Null';
    
    global Iterable<sObject> start(Database.BatchableContext info){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext info, List<Account_Territory_Mapping__c> scope){
        List<Account_List_vod__c> ToInsertAcctList = new List<Account_List_vod__c>();
        
        for(Account_Territory_Mapping__c  Atm : scope){
            if(Atm.Account_Name_String__c != Null && Atm.Account_Owner__c !=Null)
            {
                Account_List_vod__c AccList = new Account_List_vod__c();
                AccList.Name = 'HO_' + Atm.Account_Name_String__c;
                AccList.OwnerId = Atm.Account_Owner__c;
                ToInsertAcctList.add(AccList);
            }   
        }
        
        insert ToInsertAcctList;  
    }
    
    global void finish(Database.BatchableContext info){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        list<string> toAddresses = new list<string>();
        toAddresses.add('marion.c.d.legacion@accenture.com');
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('Eisai_BatchInsertAcctList Batch Job Run Complete');
        
        System.debug('Insert Account List Count: ' + ToInsertAcctList.size());
        
    }
    
}//end of batch class

Let me know if that works for you.

Best Regards,
BALAJI
This was selected as the best answer
Yoni LegacyYoni Legacy
ok, can you please guys tell me how to execute the batch job in Anonynmous window to test if that is working? This should insert more than 10k records right?
BALAJI CHBALAJI CH
You have to create an instance of your batch class and execute it. Please find below code, copy and paste in Anonymous window and click on execute. Eisai_BatchInsertAcctList abc = new Eisai_BatchInsertAcctList(); Database.executebatch(abc); Let me know if that works for you. Best Regards, BALAJI
Yoni LegacyYoni Legacy
Thanks. This is working now. Thank you Balajich