I am getting an error of Too many DML rows: 10001 on my batch class.I am putting up the code here consisting of both my scheduler class and batch class which is called through it.
Scheduler Class :-
global class PartnerLeadtoAccountScheduler implements Schedulable {
global Database.Batchable<SObject> batchClass{get;set;}
global Integer batchSize{get;set;} {batchSize = 200;}
global void execute(SchedulableContext sc) {
PartnerLeadtoAccountBatch b = new PartnerLeadtoAccountBatch();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,200);
}
}
Batch Class :-
global class PartnerLeadtoAccountBatch implements Database.Batchable<sObject>{
global PartnerLeadtoAccountBatch(){
// Batch Constructor
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
String query='Select id, Business_Nature__c, Company_Name__c, Contact_Number__c, Contact_Person__c, Date__c, Designation__c, Email_Address__c, Package_Plan__c, OwnerId, CP_Name_Text__c, createddate FROM Partner_Lead__c WHERE Email_Address__c != Null';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<sObject>scope){
// Logic to be Executed batch wise
map < Id, Account > mapAccount = new map < Id, Account > ();
set < string > checkDuplicateEmail = new set < string > ();
set < String > emailEntry = new set < String > ();
List < Partner_Lead__c > partnerLead = [Select id, Business_Nature__c, Company_Name__c, Contact_Number__c, Contact_Person__c, Date__c, Designation__c, Email_Address__c, Package_Plan__c, OwnerId, CP_Name_Text__c, createddate FROM Partner_Lead__c WHERE Email_Address__c != Null];
List < Partner_Lead__c > partnerLead1 = [Select id, Business_Nature__c, Company_Name__c, Contact_Number__c, Contact_Person__c, Date__c, Designation__c, Email_Address__c, Package_Plan__c, OwnerId, CP_Name_Text__c, createddate FROM Partner_Lead__c WHERE Email_Address__c != Null order by createddate asc];
Map < string, schema.RecordTypeInfo > rtName = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt = rtName.get('Lead').getRecordTypeId();
Map < string, schema.RecordTypeInfo > rtName1 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt1 = rtName.get('Merchant').getRecordTypeId();
mapAccount = new Map < Id, Account > ([SELECT id, Subscription_Email__c, Channel_Partner__c, Closure_Source__c, RecordTypeId FROM Account]);
for (account acc: mapAccount.values()) {
checkDuplicateEmail.add(acc.Subscription_Email__c);
}
Map < String, Partner_Lead__c[] > assotiactedServiceByEmail = new Map < String, Partner_Lead__c[] > ();
List < Partner_Lead__c > partnerleadToBeUpdated = new List < Partner_Lead__c > ();
List < Account > accountList = new List < Account > ();
List < Account > updatedAccountList = new List < Account > ();
Map < string, Account > mapSAB = new Map < string, Account > ();
Map < string, Account > mapUpdatedSAB = new Map < string, Account > ();
set<string> emailString = new set<string>();
map<string, Partner_Lead__c> partnerLeadMap = new map<string, Partner_Lead__c>();
Partner_Lead__c plead;
for (Partner_Lead__c pl: partnerLead) {
plead = new Partner_Lead__c(id = pl.id);
if (!checkDuplicateEmail.contains(pl.Email_Address__c)) {
if(pl.Contact_Person__c!=null){
Account acc = new Account();
acc.LastName = pl.Contact_Person__c;
acc.RecordTypeId = rt;
acc.Industry = pl.Business_Nature__c;
acc.Company_Name__c = pl.Company_Name__c;
acc.Phone = pl.Contact_Number__c;
acc.Designation__c = pl.Designation__c;
acc.Subscription_Email__c = pl.Email_Address__c;
acc.AccountSource = 'Channel Partner';
acc.Channel_Partner__c = True;
acc.Package_Plan__c = pl.Package_Plan__c;
acc.CP_Name__c = pl.OwnerId;
acc.Cp_Date__c = DateTime.valueOf(pl.createddate).Date();
acc.CP_Name_Text__c = pl.Company_Name__c;
accountList.add(acc);
}
}
if (checkDuplicateEmail.contains(pl.Email_Address__c)) {
emailString.add(pl.Email_Address__c);
partnerLeadMap.put(pl.Email_Address__c, pl);
}
}
list<Account> accountUpdateList = [Select Id, Subscription_Email__c, RecordTypeId from Account where Subscription_Email__c in : emailString];
for(account ac: accountUpdateList){
Map < string, schema.RecordTypeInfo > rtName2 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt2 = rtName.get('Lead').getRecordTypeId();
Map < string, schema.RecordTypeInfo > rtName3 = schema.SObjectType.Account.getRecordTypeInfosByName();
Id rt3 = rtName.get('Merchant').getRecordTypeId();
updatedAccountList.add(ac);
if (mapAccount.get(ac.Id).Closure_Source__c == 'Organic' && mapAccount.get(ac.Id).RecordTypeId == rt3) {
mapAccount.get(ac.Id).Channel_Partner__c = true;
mapAccount.get(ac.Id).Closure_Source__c = 'Channel Partner';
mapAccount.get(ac.Id).CP_Name__c = partnerLeadMap.get(ac.Subscription_Email__c).OwnerId;
mapAccount.get(ac.Id).CP_Name_Text__c = partnerLeadMap.get(ac.Subscription_Email__c).Company_Name__c;
mapAccount.get(ac.Id).Cp_Date__c = partnerLeadMap.get(ac.Subscription_Email__c).Date__c;
mapAccount.get(ac.Id).Package_Plan__c = partnerLeadMap.get(ac.Subscription_Email__c).Package_Plan__c;
} else if (mapAccount.get(ac.Id).Closure_Source__c != 'Organic' && mapAccount.get(ac.Id).RecordTypeId == rt2) {
mapAccount.get(ac.Id).Channel_Partner__c = true;
mapAccount.get(ac.Id).CP_Name__c = partnerLeadMap.get(ac.Subscription_Email__c).OwnerId;
mapAccount.get(ac.Id).CP_Name_Text__c = partnerLeadMap.get(ac.Subscription_Email__c).Company_Name__c;
mapAccount.get(ac.Id).Package_Plan__c = partnerLeadMap.get(ac.Subscription_Email__c).Package_Plan__c;
mapAccount.get(ac.Id).Cp_Date__c = partnerLeadMap.get(ac.Subscription_Email__c).Date__c;
}
}
List<Partner_Lead__c> pllist=new list<Partner_Lead__c>();
if(accountList!=null){
for (Account acc1: accountList) {
mapSAB.put(acc1.Subscription_Email__c, acc1);
}
}
for (Account acc1: updatedAccountList) {
mapUpdatedSAB.put(acc1.Subscription_Email__c, acc1);
}
if (!mapSAB.values().isEmpty()) {
insert mapSAB.values();
}
update mapAccount.values();
//Populating Account lookup on partner lead object
for(Partner_Lead__c pl :partnerLead){
if(mapSAB.get(pl.Email_Address__c)!=null){
pl.Account__c=mapSAB.get(pl.Email_Address__c).id;
}
else if(mapUpdatedSAB.get(pl.Email_Address__c)!=null){
pl.Account__c=mapUpdatedSAB.get(pl.Email_Address__c).id;
}
pllist.add(pl);
}
Integer cnt = 0;
for (Partner_Lead__c pl: partnerLead1) {
cnt++;
plead = new Partner_Lead__c(id = pl.id);
if (!(emailEntry.contains(pl.Email_Address__c))) {
plead.Updated__c = true;
emailEntry.add(pl.Email_Address__c);
} else {
plead.Conflict__c = true;
}
partnerleadToBeUpdated.add(plead);
}
update partnerleadToBeUpdated;
update pllist;
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}