You need to sign in to do that
Don't have an account?

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.
Below is the batch class which I tried to create but I encountered an error.
Below is the error messasge which I encountered:

Thanks,
Marion
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:
Thanks,
Marion
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.
Let me know if that works for you.
Best Regards,
BALAJI
All Answers
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.
Let me know if that works for you.
Best Regards,
BALAJI