You need to sign in to do that
Don't have an account?
anup-prakash
DML 10001 Error In Batch.
Hi I get an error : "First error: Too many DML rows: 10001" when the Batch Runs.
In the schedular we have tried passing 1 as the parameter even though that doesn't seem to help..
Please Help
global class BatchSetAccountOwner implements Database.Batchable<sObject>{
global list<Account> accList = new list<Account>();
global list<Task> taskList = new list<Task>();
global list<Account> accountList = new list<Account>();
global list<Opportunity> opportunityList = new list<Opportunity>();
global list<Account> accountListToUpdate = new list<Account>();
global list<Opportunity> opportunityListToUpdate = new list<Opportunity>();
global String query;
global set<Id> excludedAccountsId = new set<Id>();
global set<Id> exculdedOpportunitiesId = new set<Id>();
global map<Id,Id> accountIdToOwnerIdMap = new map<Id,Id> ();
global Date todaysDate = Date.today();
global Date oldDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
global Date openTaskDate= todaysDate.addDays(Integer.ValueOf(System.Label.openTaskDays));
global Date threeMonthsFromTodaysDate = todaysDate.addMonths(Integer.ValueOf(System.Label.OpportunityEndMonth));
global String marketingUserId = System.label.MarketingUserID;
global List<Date> listOfDateToSelect = new List<Date>();
global Database.QueryLocator start(Database.BatchableContext BC){
query = 'Select OwnerId, Id, Name,Type,(Select Id, CloseDate, AccountId, OwnerId, Owner.ManagerId FROM Opportunities WHERE CloseDate >= :todaysDate AND CloseDate <= :threeMonthsFromTodaysDate AND StageName != \'Closed Won\' AND StageName != \'Closed Won\') From Account WHERE OwnerId != :marketingUserId';
System.debug('query ::'+ query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Integer daystoSlect = Integer.ValueOf(System.Label.OldTaskDays) +Integer.ValueOf(System.Label.openTaskDays);
date tempDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
for(Integer i=0; i< daystoSlect ; i++){
tempDate = tempDate.addDays(1);
listOfDateToSelect.add(tempDate);
}
accList = (list<Account>)scope; // TypeCast to ListOfAccount
System.debug('accList ::'+accList);
if(!accList.isEmpty()){
System.debug('oldDate ::'+oldDate + 'openTaskDate ::'+openTaskDate);
system.debug('listOfDateToSelect ::' + listOfDateToSelect);
taskList = [SELECT AccountId, ActivityDate, WhatId, WhoId, isClosed FROM Task WHERE ActivityDate != null AND ActivityDate IN :listOfDateToSelect AND AccountId !=null AND Account.OwnerId !=:marketingUserId];
System.debug('taskList ::'+taskList);
for(Account accObj : accList){
System.debug('accObj ::'+accObj);
// Work For Tasks
if(!taskList.isEmpty()){
for(Task task : taskList){
//system.debug( 'oldDate' +oldDate+ 'openTaskDate' +openTaskDate);
DateTime dt = task.ActivityDate;
Date taskActivityDate = dt.Date();
if(task.AccountId == accObj.Id && ((taskActivityDate >oldDate && task.IsClosed) || (taskActivityDate < openTaskDate && taskActivityDate > todaysDate && task.IsClosed==false))){
system.debug( 'taskActivityDate ::' +taskActivityDate);
system.debug('task.AccountId ::'+task.AccountId);
excludedAccountsId.add(task.AccountId);
}
}
}
// Work For Opportunities
if(accObj.Opportunities.size() > 0){
for(Opportunity opp : accObj.Opportunities){
excludedAccountsId.add(opp.AccountId);
//exculdedOpportunitiesId.add(opp.Id);
}
}
// Exclude Accounts of type Subscriber
if(accObj.Type == 'Subscriber'){
excludedAccountsId.add(accObj.Id);
}
}
System.debug('excludedAccountsId ::'+excludedAccountsId);
accountList = [Select OwnerId from Account Where Id Not IN :excludedAccountsId ];
System.debug('accountList to Update :: ' + accountList);
//opportunityList = [SELECT o.OwnerId, o.Owner.ManagerId FROM Opportunity o WHERE Id NOT IN :exculdedOpportunitiesId];
for(Account acc : accountList){
acc.OwnerId = marketingUserId;
System.debug('***marketingUserId*****'+ marketingUserId);
system.debug('*****acc.OwnerId******'+ acc.OwnerId);
accountListToUpdate.add(acc);
}
/*for(Opportunity opp : opportunityList){
if(opp.Owner.ManagerId !=null){
system.debug('***Opp Owner Manager**' + opp.Owner.ManagerId);
opp.OwnerId = opp.Owner.ManagerId;
opportunityListToUpdate.add(opp);
}
}*/
try{
update accountListToUpdate;
System.debug('***accountListToUpdate***'+ accountListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
/*
try{
update opportunityListToUpdate;
System.debug('***opportunityListToUpdate***'+ opportunityListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
*/
}
}
global void finish(Database.BatchableContext BC){
System.debug('*******************FINISH**********************');
}
}
In the schedular we have tried passing 1 as the parameter even though that doesn't seem to help..
Please Help
global class BatchSetAccountOwner implements Database.Batchable<sObject>{
global list<Account> accList = new list<Account>();
global list<Task> taskList = new list<Task>();
global list<Account> accountList = new list<Account>();
global list<Opportunity> opportunityList = new list<Opportunity>();
global list<Account> accountListToUpdate = new list<Account>();
global list<Opportunity> opportunityListToUpdate = new list<Opportunity>();
global String query;
global set<Id> excludedAccountsId = new set<Id>();
global set<Id> exculdedOpportunitiesId = new set<Id>();
global map<Id,Id> accountIdToOwnerIdMap = new map<Id,Id> ();
global Date todaysDate = Date.today();
global Date oldDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
global Date openTaskDate= todaysDate.addDays(Integer.ValueOf(System.Label.openTaskDays));
global Date threeMonthsFromTodaysDate = todaysDate.addMonths(Integer.ValueOf(System.Label.OpportunityEndMonth));
global String marketingUserId = System.label.MarketingUserID;
global List<Date> listOfDateToSelect = new List<Date>();
global Database.QueryLocator start(Database.BatchableContext BC){
query = 'Select OwnerId, Id, Name,Type,(Select Id, CloseDate, AccountId, OwnerId, Owner.ManagerId FROM Opportunities WHERE CloseDate >= :todaysDate AND CloseDate <= :threeMonthsFromTodaysDate AND StageName != \'Closed Won\' AND StageName != \'Closed Won\') From Account WHERE OwnerId != :marketingUserId';
System.debug('query ::'+ query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Integer daystoSlect = Integer.ValueOf(System.Label.OldTaskDays) +Integer.ValueOf(System.Label.openTaskDays);
date tempDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
for(Integer i=0; i< daystoSlect ; i++){
tempDate = tempDate.addDays(1);
listOfDateToSelect.add(tempDate);
}
accList = (list<Account>)scope; // TypeCast to ListOfAccount
System.debug('accList ::'+accList);
if(!accList.isEmpty()){
System.debug('oldDate ::'+oldDate + 'openTaskDate ::'+openTaskDate);
system.debug('listOfDateToSelect ::' + listOfDateToSelect);
taskList = [SELECT AccountId, ActivityDate, WhatId, WhoId, isClosed FROM Task WHERE ActivityDate != null AND ActivityDate IN :listOfDateToSelect AND AccountId !=null AND Account.OwnerId !=:marketingUserId];
System.debug('taskList ::'+taskList);
for(Account accObj : accList){
System.debug('accObj ::'+accObj);
// Work For Tasks
if(!taskList.isEmpty()){
for(Task task : taskList){
//system.debug( 'oldDate' +oldDate+ 'openTaskDate' +openTaskDate);
DateTime dt = task.ActivityDate;
Date taskActivityDate = dt.Date();
if(task.AccountId == accObj.Id && ((taskActivityDate >oldDate && task.IsClosed) || (taskActivityDate < openTaskDate && taskActivityDate > todaysDate && task.IsClosed==false))){
system.debug( 'taskActivityDate ::' +taskActivityDate);
system.debug('task.AccountId ::'+task.AccountId);
excludedAccountsId.add(task.AccountId);
}
}
}
// Work For Opportunities
if(accObj.Opportunities.size() > 0){
for(Opportunity opp : accObj.Opportunities){
excludedAccountsId.add(opp.AccountId);
//exculdedOpportunitiesId.add(opp.Id);
}
}
// Exclude Accounts of type Subscriber
if(accObj.Type == 'Subscriber'){
excludedAccountsId.add(accObj.Id);
}
}
System.debug('excludedAccountsId ::'+excludedAccountsId);
accountList = [Select OwnerId from Account Where Id Not IN :excludedAccountsId ];
System.debug('accountList to Update :: ' + accountList);
//opportunityList = [SELECT o.OwnerId, o.Owner.ManagerId FROM Opportunity o WHERE Id NOT IN :exculdedOpportunitiesId];
for(Account acc : accountList){
acc.OwnerId = marketingUserId;
System.debug('***marketingUserId*****'+ marketingUserId);
system.debug('*****acc.OwnerId******'+ acc.OwnerId);
accountListToUpdate.add(acc);
}
/*for(Opportunity opp : opportunityList){
if(opp.Owner.ManagerId !=null){
system.debug('***Opp Owner Manager**' + opp.Owner.ManagerId);
opp.OwnerId = opp.Owner.ManagerId;
opportunityListToUpdate.add(opp);
}
}*/
try{
update accountListToUpdate;
System.debug('***accountListToUpdate***'+ accountListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
/*
try{
update opportunityListToUpdate;
System.debug('***opportunityListToUpdate***'+ opportunityListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
*/
}
}
global void finish(Database.BatchableContext BC){
System.debug('*******************FINISH**********************');
}
}
You should not have any DML statements in the for loop.
* -> accountListToUpdate.add(acc);
move it out of the loop and try again.
See more information in the links below,
https://developer.salesforce.com/forums/ForumsMain?id=906F000000092BbIAI
http://salesforce.stackexchange.com/questions/15228/how-to-avoid-dml-insert-update-limit-of-10-000-rows-when-running-anonymous-apex
http://salesforce.stackexchange.com/questions/12514/system-limitexception-too-many-dml-rows-10001
Regards,
Ashish
But I guess that the List being Updated ain't inside a loop.
The line of code that i mentioned does falll under the For loop,
I have changed the code, see if that works,
global class BatchSetAccountOwner implements Database.Batchable<sObject>{
global list<Account> accList = new list<Account>();
global list<Task> taskList = new list<Task>();
global list<Account> accountList = new list<Account>();
global list<Opportunity> opportunityList = new list<Opportunity>();
global list<Account> accountListToUpdate = new list<Account>();
global list<Opportunity> opportunityListToUpdate = new list<Opportunity>();
global String query;
global set<Id> excludedAccountsId = new set<Id>();
global set<Id> exculdedOpportunitiesId = new set<Id>();
global map<Id,Id> accountIdToOwnerIdMap = new map<Id,Id> ();
global Date todaysDate = Date.today();
global Date oldDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
global Date openTaskDate= todaysDate.addDays(Integer.ValueOf(System.Label.openTaskDays));
global Date threeMonthsFromTodaysDate = todaysDate.addMonths(Integer.ValueOf(System.Label.OpportunityEndMonth));
global String marketingUserId = System.label.MarketingUserID;
global List<Date> listOfDateToSelect = new List<Date>();
global Database.QueryLocator start(Database.BatchableContext BC){
query = 'Select OwnerId, Id, Name,Type,(Select Id, CloseDate, AccountId, OwnerId, Owner.ManagerId FROM Opportunities WHERE CloseDate >= :todaysDate AND CloseDate <= :threeMonthsFromTodaysDate AND StageName != \'Closed Won\' AND StageName != \'Closed Won\') From Account WHERE OwnerId != :marketingUserId';
System.debug('query ::'+ query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Integer daystoSlect = Integer.ValueOf(System.Label.OldTaskDays) +Integer.ValueOf(System.Label.openTaskDays);
date tempDate = todaysDate.addDays(-Integer.ValueOf(System.Label.OldTaskDays));
for(Integer i=0; i< daystoSlect ; i++){
tempDate = tempDate.addDays(1);
listOfDateToSelect.add(tempDate);
}
accList = (list<Account>)scope; // TypeCast to ListOfAccount
System.debug('accList ::'+accList);
if(!accList.isEmpty()){
System.debug('oldDate ::'+oldDate + 'openTaskDate ::'+openTaskDate);
system.debug('listOfDateToSelect ::' + listOfDateToSelect);
taskList = [SELECT AccountId, ActivityDate, WhatId, WhoId, isClosed FROM Task WHERE ActivityDate != null AND ActivityDate IN :listOfDateToSelect AND AccountId !=null AND Account.OwnerId !=:marketingUserId];
System.debug('taskList ::'+taskList);
for(Account accObj : accList){
System.debug('accObj ::'+accObj);
// Work For Tasks
if(!taskList.isEmpty()){
for(Task task : taskList){
//system.debug( 'oldDate' +oldDate+ 'openTaskDate' +openTaskDate);
DateTime dt = task.ActivityDate;
Date taskActivityDate = dt.Date();
if(task.AccountId == accObj.Id && ((taskActivityDate >oldDate && task.IsClosed) || (taskActivityDate < openTaskDate && taskActivityDate > todaysDate && task.IsClosed==false))){
system.debug( 'taskActivityDate ::' +taskActivityDate);
system.debug('task.AccountId ::'+task.AccountId);
excludedAccountsId.add(task.AccountId);
}
}
}
// Work For Opportunities
if(accObj.Opportunities.size() > 0){
for(Opportunity opp : accObj.Opportunities){
excludedAccountsId.add(opp.AccountId);
//exculdedOpportunitiesId.add(opp.Id);
}
}
// Exclude Accounts of type Subscriber
if(accObj.Type == 'Subscriber'){
excludedAccountsId.add(accObj.Id);
}
}
System.debug('excludedAccountsId ::'+excludedAccountsId);
accountList = [Select OwnerId from Account Where Id Not IN :excludedAccountsId ];
System.debug('accountList to Update :: ' + accountList);
//opportunityList = [SELECT o.OwnerId, o.Owner.ManagerId FROM Opportunity o WHERE Id NOT IN :exculdedOpportunitiesId];
for(Account acc : accountList){
acc.OwnerId = marketingUserId;
System.debug('***marketingUserId*****'+ marketingUserId);
system.debug('*****acc.OwnerId******'+ acc.OwnerId);
}
accountListToUpdate.add(acc);
/*for(Opportunity opp : opportunityList){
if(opp.Owner.ManagerId !=null){
system.debug('***Opp Owner Manager**' + opp.Owner.ManagerId);
opp.OwnerId = opp.Owner.ManagerId;
}
opportunityListToUpdate.add(opp);
}*/
try{
update accountListToUpdate;
System.debug('***accountListToUpdate***'+ accountListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
/*
try{
update opportunityListToUpdate;
System.debug('***opportunityListToUpdate***'+ opportunityListToUpdate);
}catch(Exception ex){
system.debug('*********' +ex.getMessage());
}
*/
}
}
global void finish(Database.BatchableContext BC){
System.debug('*******************FINISH**********************');
}
}
Regards,
Ashish
I can see all your DML statement are outside the forloop. So there is so possibility to get this Error.
Could you please tell me do you have any Trigger on Account or Opportunity object. if so please check
those two objects code.
Thanks,
Saravanan
The code was posted by Anup in which the DML statement was inside the forloop.
I had modified the code and pasted above.
Looks like the issue has been taken care of.
Regards,
Ashish