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
SYM12SYM12 

System.Exception: Too many DML rows:

Hi All,

 

I am updating opportunities while updating the Account Owner.

 

Opportunities are more than 100 , so i divided into batches of 50 

 

but i am still getting the DML execption while updating 300 records ( max DML satatements should be 6)

 

\Error: Apex trigger PAMUpdate caused an unexpected exception, contact your administrator: PAMUpdate: execution of BeforeUpdate caused by: System.Exception: Too many DML rows: 147: Trigger.PAMUpdate: line 63, column 5

 

 Here is my complete snipped of code

 

trigger PAMUpdate on Account (before update) {

    Map <Id, String> acc = new Map <Id, String>();

    List<String> accountList = new List<String>();

    for (Account a : Trigger.new)

    {

        String oppOwner = Trigger.oldMap.get(a.Id).OwnerId;

        if (a.IsPartner == true && a.OwnerId != oppOwner)

        {           

            accountList.add(a.Id);

        }

    }
  //  System.debug(' Accounts are ' + accountList );
   // System.debug(' Account Size is =  ' + accountList.size() );
 
   
 List<Opportunity> oppList = new List<Opportunity>();

    if(accountList.size()>0)

    {  

        oppList  = [select PAM__c,PartnerAccountId from Opportunity

                                     where PartnerAccountId In : accountList limit 300];

        }
        for(Opportunity oppRec:oppList)

        {

            oppRec.PAM__c = Trigger.newMap.get(oppRec.PartnerAccountId).OwnerId;

        }

       
 if(oppList.size() > 50){
       List<Opportunity> subList = new List<Opportunity>();

for(Opportunity record : oppList){

subList.add(record);

if(subList.size()==49){

try{
    update subList;  }
    catch(Exception ex){
            System.debug(' Exception Occured' + ex);}
subList.clear();
}
}
if(subList.size() > 0){

try{
    update subList;  }
    catch(Exception ex){
            System.debug(' Exception Occured' + ex);}
}

}else{

try{
    update oppList;  }
    catch(Exception ex){
            System.debug(' Exception Occured' + ex);}

} }

 

Please suggest if i am doing any thing wrong while updating .

 

Thanks in advance

XactiumBenXactiumBen
Your problem is not the number of DML statements but the number of records you are retrieving from your SOQL statement. I believe the issue is that you're trying to get 147 opportunities out of one account when the limit only allows for 100 opportunities per account. Don't think there is anything you can do about this in a trigger unless you want to go down the route of using batch apex.
SYM12SYM12

i am use the batches of 50 so can't i upadte the record upto 300 with 6 batches of 50 ( as it will include on 6 DML statements) . As per my understanding i can update 100 records in 1 batch.

 
mtbclimbermtbclimber

XactiumBen is correct, if your trigger sees one record you can perform DML on a maxiumum of 100 rows - regardless of how many DML statements you use to process the records.  If the cardinality of your data is not suited to fit within this scope you have a couple of options.

 

You can start using Async apex to process larger chunks but if the data could be reasonably unbounded you should investigate Batch Apex.

SYM12SYM12

Andrew, Actually problem is that i have more than 1000 opportunities to update. I was just giving a trial with 300. If trigger has limit to update 100 DML at a time , so can't we create batches of 100 ( Beacuse the DML Statement limit is 20) . so with that we can update 100 *20 = 20,000 records.

 

Please suggest

mtbclimbermtbclimber
I don't have anything to suggest beyond my last message above.
dmchengdmcheng
Do your account owners change so often that you need a trigger?  You may need to give up on the trigger and do manual Data Loader updates instead.
SYM12SYM12

Yeah  it happns frequently and we need some automation...........

 

Any other solution??

learnSFlearnSF