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
ArunaAruna 

System.LimitException: Too many DML rows: 10001(can any one help me on this)

I am getting this error when i am trying to update.

 

list

<Account> accList;

 

list<Account> accountUpdate=newlist<Account>();

 

if(recordOwnerString()=='Owner'){

accList=[

select id,Name,OwnerId fromAccountwhereOwnerId=:selectedOwnerId];

 

// if(accList.size()>0){for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,OwnerId=secondUserId());

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate; ---> getting error on this line

}

//}

 

 

Scott_VSScott_VS

You're updating too many accounts.

 

Try using Batch Apex instead for handling large data sets.

ArunaAruna

Thanks for quick replay , could you please send me an example.

 

How can i write batch for my code.

 

 

Scott_VSScott_VS

Here is the documentation from SFDC with some coding examples:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

ArunaAruna

ok, Can we write a more than one execute method in batch class and call that execute methods from other normal apex class method in different class.

 

what i mean is , I need to assign Lead,Contact and account records from one user to other user based on the buttons on the visual force page.

i,e if the user clicks on Transfer lead , lead need to be transfered from one user to other user and same for contact and accounts also

 

so I am thingking I can do this in apex batch , how can i call this methodes on button.

if you have any sample code could you please send me .

Scott_VSScott_VS

You can only write one execute method in a batch class, but you have flexibility in the passing variables to the batch class constructor. See the "UpdateAccountFields" class example in the link above for an example of a batch class that has variables being passed, then you would use a line like:

 

Database.executeBatch(new UpdateAccountFields(q,e,f,v)); 

 

in your button Apex code to start the batch.

 

Note that when you call the Database.executeBatch() line from your button, it will put the batch code in a queue, so the change will NOT be immediate. That's the trade off for using a batch apex. You'll have more resources available, but you will have to wait for them. Once the batch starts going, this is what the system does:

 

1) Calls the constructor.

2) Calls the start() method to run a SOQL query and splits the results into "batches" of 200 rows.

3) Repeats the execute() method for each "batch". So if you have 10,000 rows in total, the execute() method will run 50 times (50 batches * 200 rows = 10,000 rows).

4) Calls the finish() method just once.

ArunaAruna

But the problem is  ,

 

ok let me give you the code what i am trying to do

 

Below is my main class which is used on visualforce page (OwnerRealignment) and this calss also call an other class called "OwnerRealignment_Account_Contact_Lead".

 

So according to your suggestion, i have to write " OwnerRealignment_Account_Contact_Lead" as a batch class.

 

if you look at that class it is having 3 different queries , basicall i ma not getting how can i put my "OwnerRealignment_Account_Contact_Lead" class to batch.

could you plehelp out me on this how can put my class into batch , if you have time colu you please send me code or steps how to my class in batch and how can i clal those method from  "wnerRealignment_Controller " methods.

 

public

class OwnerRealignment_Controller {

 

OwnerRealignment_Account_Contact_Lead OwnerRealiment=

newOwnerRealignment_Account_Contact_Lead();

 

publicvoidsetOwnerRealiment(OwnerRealignment_Account_Contact_Lead OwnerRealiment){

 

this.OwnerRealiment=OwnerRealiment;

}

publicOwnerRealignment_Account_Contact_Lead getOwnerRealiment(){

 

returnOwnerRealiment;

}

publicpageReference transferAccount(){

OwnerRealiment.updateAccountOwner();

returnnull;

}

publicpageReference transferContact(){

OwnerRealiment.updateContactOwner();

returnnull;

}

publicpageReference transferLead(){

OwnerRealiment.updateLeadOwner();

returnnull;

}

publicpageReference tarnsferAllLeads(){

 

OwnerRealiment.updateAccountOwner();

OwnerRealiment.updateContactOwner();

OwnerRealiment.updateLeadOwner();

returnnull;

}

}

 

public

class OwnerRealignment_Account_Contact_Lead { //Implements Database.Batchable<sObject> {publiclist<User> userList{get;set;}

 

public string userAName{get;set;}//TODO : Rename this Fieldpublic string userBName{get;set;}//TODO : Rename this Fieldpublic string recordOwnerString{get;set;}//TODO : Rename this Fieldpublic string realignment{get;set;}

 

publicset<Id> userIds=newset<Id>();

 

publicOwnerRealignment_Account_Contact_Lead(){

 

}

publiclist<selectOption> getactiveUsersList(){

 

list<selectOption> userOptionList=newlist<selectOption>();

userList=[

select id,Name,isActive fromUserwhere isActive=trueorderByName];

 

for(Useru : userList){

userOptionList.add(

new selectOption(u.id,u.Name));

}

returnuserOptionList;

}

publiclist<selectOption> getrecordOwners(){

 

list<selectOption> recordOwnerOption=newlist<selectOption>();

 

recordOwnerOption.add(

new selectOption('Owner','Owner'));

recordOwnerOption.add(

new selectOption('Secondary VP','Secondary VP'));

recordOwnerOption.add(

new selectOption('Account Executive','Account Executive'));

recordOwnerOption.add(

new selectOption('Secondary Account Executive','Secondary Account Executive'));

 

returnrecordOwnerOption;

}

//TODO : Rename this methodprivateId firstUserId(){

userAName=userAName.replace(

'[','');

userAName=userAName.replace(

']','');

 

returnuserAName;

}

//TODO : Rename this methodprivateId secondUserId(){

userBName=userBName.replace(

'[','');

userBName=userBName.replace(

']','');

 

returnuserBName;

}

privatestring recordOwnerString(){

recordOwnerString=recordOwnerString.replace(

'[','');

recordOwnerString=recordOwnerString.replace(

']','');

 

returnrecordOwnerString;

}

publicvoidupdateAccountOwner(){

 

list<Account> accList;

 

list<Account> accountUpdate=newlist<Account>();

 

 

if(recordOwnerString()=='Owner'){

accList=[

select id,Name,OwnerId fromAccountwhereOwnerId=:firstUserId()];

 

// if(accList.size()>0){for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,OwnerId=secondUserId());

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

//}

}

elseif(recordOwnerString()=='Secondary VP'){

accList=[

select id,Name,Secondary_VP__c fromAccountwhereSecondary_VP__c=:firstUserId()];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,Secondary_VP__c=secondUserId());

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

}

elseif(recordOwnerString()=='Account Executive'){

accList=[

select id,Name,AccountManager__c fromAccountwhereAccountManager__c=:firstUserId()];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,AccountManager__c=secondUserId());

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

 

}

else{

accList=[

select id,Name,AccountManager_Secondary__c fromAccountwhereAccountManager_Secondary__c=:firstUserId()];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,AccountManager_Secondary__c=secondUserId());

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

 

}

}

}

else{

conList=[

select id,Name,AccountManager_Secondary__c fromContactwhereAccountManager_Secondary__c=:firstUserId()];

 

if(conList.size()>0){

 

for(ContactconRec : conList){

 

Contact con=newContact(id=conRec.id,AccountManager_Secondary__c=secondUserId());

contactUpdate.add(con);

}

if(contactUpdate.size()>0){

 

updatecontactUpdate;

}

}

}

}

publicvoidupdateLeadOwner(){

 

list<Lead> leadList;

 

list<Lead> leadUpdateList=newlist<Lead>();

 

if(recordOwnerString()=='Owner'){

 

leadList=[

select id,Name,OwnerId fromLeadwhere OwnerId=:firstUserId() and isConverted=false];

 

if(leadList.size()>0){

 

for(LeadleadRec : leadList){

 

Lead lead=newLead(id=leadRec.id,OwnerId=secondUserId());

leadUpdateList.add(lead);

}

if(leadUpdateList.size()>0){

 

updateleadUpdateList;

}

}

}

elseif(recordOwnerString()=='Account Executive'){

leadList=[

select id,Name,Account_Executive__c fromLeadwhereAccount_Executive__c=:firstUserId()];

 

if(leadList.size()>0){

 

for(LeadleadRec : leadList){

 

Lead lead=newLead(id=leadRec.id,Account_Executive__c=secondUserId());

leadUpdateList.add(lead);

}

if(leadUpdateList.size()>0){

 

updateleadUpdateList;

}

}

 

}

}

}

 

OwnerRealignment_Page

 

<apex:page controller="OwnerRealignment_Controller" id="OwnerRealignment_Page">

      <apex:form id="OwnerRealignment_Form" title="OwnerRealignment">

      <apex:pageBlock title="Owner Realignment">

           

            <apex:pageBlockSection columns="2">

              

              <apex:outputLabel title="Need to Defined"  value="Need to Define"/>

              <apex:selectList value="{!OwnerRealignment_Account_Contact_Lead.recordOwnerString}" size="4" multiselect="true">

                  <apex:selectOptions value="{!OwnerRealignment_Account_Contact_Lead.recordOwners}"/>

              </apex:selectList>

             

              </apex:pageBlockSection><br/><br/>

 

              <apex:pageBlockSection columns="12">

             

              <apex:outputLabel title="Select User A"  value="Select User A"/>

              <apex:selectList value="{!OwnerRealignment_Account_Contact_Lead.userAName}" size="10" multiselect="true">

                  <apex:selectOptions value="{!OwnerRealignment_Account_Contact_Lead.activeUsersList}"/>

              </apex:selectList>

             

               <apex:outputLabel title="Select User B" value="Select User B"/>

              <apex:selectList value="{!OwnerRealignment_Account_Contact_Lead.userBName}" size="10" multiselect="true">

                  <apex:selectOptions value="{!OwnerRealignment_Account_Contact_Lead.activeUsersList}"/>

              </apex:selectList>

             

              </apex:pageBlockSection><br/><br/>

                 

                  <apex:pageBlockSection columns="6">

                        <apex:outputLabel value="Realignment"/>

                  <apex:inputText value="{!OwnerRealignment_Account_Contact_Lead.realignment}"/>

           

            </apex:pageBlockSection><br/><br/>

                 

                  <center>

                  <apex:commandButton action="{!transferAccount}"  title="Transfer Account" value="Account"/>

                                  <apex:commandButton action="{!transferContact}"  title="Transfer Contact" value="Contact"/>

                                  <apex:commandButton action="{!transferLead}"  title="Transfer Lead" value="Lead"/><br/><br/>

                                     

                     OR<br/><br/>

                 

                                  <apex:commandButton action="{!tarnsferAllLeads}"  title="Transfer Lead" value="Transfer All Lead"/>

           </center>

        </apex:pageBlock>

    </apex:form>

 

</apex:page>

 

ArunaAruna

Can any one please give me solution to this problem, I also used future method nutsstill itis throwing same error too Many DML 10001

 

 

@

future

publicstaticvoidupdateAccountOwner(string recordOwnerString,Id firstUserId,Id secondUserId){

 

list<Account> accList;

 

list<Account> accountUpdate=newlist<Account>();

 

if(recordOwnerString=='Owner'){

accList=[

select id,Name,OwnerId fromAccountwhereOwnerId=:firstUserId];

 

// if(accList.size()>0){for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,OwnerId=secondUserId);

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

//}

}

elseif(recordOwnerString=='Secondary VP'){

accList=[

select id,Name,Secondary_VP__c fromAccountwhereSecondary_VP__c=:firstUserId];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,Secondary_VP__c=secondUserId);

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

}

elseif(recordOwnerString=='Account Executive'){

accList=[

select id,Name,AccountManager__c fromAccountwhereAccountManager__c=:firstUserId];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,AccountManager__c=secondUserId);

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

 

}

else{

accList=[

select id,Name,AccountManager_Secondary__c fromAccountwhereAccountManager_Secondary__c=:firstUserId];

 

if(accList.size()>0){

 

for(AccountaccRec : accList){

 

Account acc=newAccount(id=accRec.id,AccountManager_Secondary__c=secondUserId);

accountUpdate.add(acc);

}

if(accountUpdate.size()>0){

 

updateaccountUpdate;

}

}

 

}

}

(string recordOwnerString,Id firstUserId,Id secondUserId){

 

list<Lead> leadList;

 

list<Lead> leadUpdateList=newlist<Lead>();

 

if(recordOwnerString=='Owner'){

 

leadList=[

select id,Name,OwnerId fromLeadwhere OwnerId=:firstUserId and isConverted=false];

 

if(leadList.size()>0){

 

for(LeadleadRec : leadList){

 

Lead lead=newLead(id=leadRec.id,OwnerId=secondUserId);

leadUpdateList.add(lead);

}

if(leadUpdateList.size()>0){

 

updateleadUpdateList;

}

}

}

elseif(recordOwnerString=='Account Executive'){

leadList=[

select id,Name,Account_Executive__c fromLeadwhereAccount_Executive__c=:firstUserId];

 

if(leadList.size()>0){

 

for(LeadleadRec : leadList){

 

Lead lead=newLead(id=leadRec.id,Account_Executive__c=secondUserId);

leadUpdateList.add(lead);

}

if(leadUpdateList.size()>0){

 

updateleadUpdateList;

}

}

 

}

}

}

 

public

staticlist<User> userList{get;set;}

 

publicstatic string userAName{get;set;}//TODO : Rename this Fieldpublicstatic string userBName{get;set;}//TODO : Rename this Fieldpublicstatic string recordOwnerString{get;set;}//TODO : Rename this Fieldpublicstatic string realignment{get;set;}

 

publicset<Id> userIds=newset<Id>();

 

publicstaticlist<selectOption> getactiveUsersList(){

 

list<selectOption> userOptionList=newlist<selectOption>();

userList=[

select id,Name,isActive fromUserwhere isActive=trueorderByName];

 

for(Useru : userList){

userOptionList.add(

new selectOption(u.id,u.Name));

}

returnuserOptionList;

}

publicstaticlist<selectOption> getrecordOwners(){

 

list<selectOption> recordOwnerOption=newlist<selectOption>();

 

recordOwnerOption.add(

new selectOption('Owner','Owner'));

recordOwnerOption.add(

new selectOption('Secondary VP','Secondary VP'));

recordOwnerOption.add(

new selectOption('Account Executive','Account Executive'));

recordOwnerOption.add(

new selectOption('Secondary Account Executive','Secondary Account Executive'));

 

returnrecordOwnerOption;

}

//TODO : Rename this methodprivatestaticId firstUserId(){

userAName=userAName.replace(

'[','');

userAName=userAName.replace(

']','');

 

returnuserAName;

}

//TODO : Rename this methodprivatestaticId secondUserId(){

userBName=userBName.replace(

'[','');

userBName=userBName.replace(

']','');

 

returnuserBName;

}

publicstaticstring recordOwnerString(){

recordOwnerString=recordOwnerString.replace(

'[','');

recordOwnerString=recordOwnerString.replace(

']','');

 

returnrecordOwnerString;

}

//string recordOwnerString,Id firstUserId,Id secondUserIdpublicOwnerRealignment_Account_Contact_Lead getOwnerRealignment_Account_Contact_Lead() {

 

returnnewOwnerRealignment_Account_Contact_Lead();

}

publicpageReference transferAccount(){

 

 

OwnerRealignment_Account_Contact_Lead.updateAccountOwner(recordOwnerString(),firstUserId(),secondUserId());

returnnull;

}

publicpageReference transferContact(){

OwnerRealignment_Account_Contact_Lead.updateContactOwner(recordOwnerString(),firstUserId(),secondUserId());

returnnull;

}

publicpageReference transferLead(){

OwnerRealignment_Account_Contact_Lead.updateLeadOwner(recordOwnerString(),firstUserId(),secondUserId());

returnnull;

}

publicpageReference tarnsferAllLeads(){

 

OwnerRealignment_Account_Contact_Lead.updateAccountOwner(recordOwnerString(),firstUserId(),secondUserId());

OwnerRealignment_Account_Contact_Lead.updateContactOwner(recordOwnerString(),firstUserId(),secondUserId());

OwnerRealignment_Account_Contact_Lead.updateLeadOwner(recordOwnerString(),firstUserId(),secondUserId());

returnnull;

}

 

ArunaAruna

Hello,

 

I think there is no use of btach class also , I am getting same error.

Plzzzzzzzzzzzzzzzzzzzzzz help me any one on this problem , i need veryDesperately.

 

global  class OwnerRealignment_Account_Contact_Lead Implements Database.Batchable<sObject> {

     

      public string query;

      public list<Account> accList{get;set;}

      public list<Account> acListUpdate=new List<Account>();

      public Database.BatchableContext BC;

     

      public static list<User> userList{get;set;}

      public  static string userAName{get;set;}//TODO : Rename this Field

      public static string userBName{get;set;}//TODO : Rename this Field

      public  static string recordOwnerString{get;set;}//TODO : Rename this Field

      public static string realignment{get;set;}

      public set<Id> userIds=new set<Id>();

     

      list<sObject> sobj=new list<Sobject>();

     

      public list<sObject> SobjectList(){

           

            sobj.add(new Account());

            sobj.add(new Contact());

            sobj.add(new Lead());

           

            system.debug('****************sobj='+sobj);

            return sobj;

      }

     

     

     

      public static list<selectOption> getactiveUsersList(){

            list<selectOption> userOptionList=new list<selectOption>();

            userList=[select id,Name,isActive from User where isActive=true order By Name];

            for(User u : userList){

                  userOptionList.add(new selectOption(u.id,u.Name));

            }

            return  userOptionList;

      }

     

      public static list<selectOption> getrecordOwners(){

           

            list<selectOption> recordOwnerOption=new list<selectOption>();

           

            recordOwnerOption.add(new selectOption('Owner','Owner'));

            recordOwnerOption.add(new selectOption('Secondary VP','Secondary VP'));

            recordOwnerOption.add(new selectOption('Account Executive','Account Executive'));

            recordOwnerOption.add(new selectOption('Secondary Account Executive','Secondary Account Executive'));

           

            return recordOwnerOption;

      }

     

      //TODO : Rename this method

      private static Id firstUserId(){

            userAName=userAName.replace('[','');

            userAName=userAName.replace(']','');

            return userAName;

      }

      //TODO : Rename this method

      private static Id secondUserId(){

            userBName=userBName.replace('[','');

            userBName=userBName.replace(']','');

            return userBName;

      }

      public  static string recordOwnerString(){

            recordOwnerString=recordOwnerString.replace('[','');

            recordOwnerString=recordOwnerString.replace(']','');

            return recordOwnerString;

      }

     

      //string recordOwnerString,Id firstUserId,Id secondUserId

      public OwnerRealignment_Account_Contact_Lead getOwnerRealignment_Account_Contact_Lead() {

        return new OwnerRealignment_Account_Contact_Lead();

    }

     

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

            return Database.getQueryLocator(query);

      }

     

           

      global void execute(Database.BatchableContext BC, List<sObject> scope){

             List<Account> accns = new List<Account>();

             List<Contact> cons = new List<Contact>();

             List<Lead> leads = new List<Lead>();

           

            for(Sobject obj :  scope){

                       

                       

                        if(obj.getSObjectType()==Account.Schema.Sobjecttype){

                              if(recordOwnerString()=='Owner'){

                                    Account acc = (Account)obj;

                                                                        accList=[select id,Name,OwnerId from Account where OwnerId=:firstUserId()];

                                          for(Account accRec : accList){

                                                Account accRecUpdate=new Account(id=accRec.id,OwnerId=secondUserId());

                                                acListUpdate.add(accRecUpdate);

                                          }

                                          if(acListUpdate.size()>0){

                                                update acListUpdate;

                                               

                                          }

                              }

                        }//Account

            }//Sobject

      }//execute

 

      global void finish(Database.BatchableContext BC){

     

      }

     

      public pageReference transferAccount(){

           

            execute(Bc,SobjectList());

           

      return null;

      }

     

     

}