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
Chirag MehtaChirag Mehta 

Whats the Solution to Perform Update of 100+ Records in Apex

Scrolled down many of posts here in Community but couldnt find one solution on how to avoid 'Too many DML rows' error.
 
Probably am i missing some community link then pls help me to get to that solution.
 
I am looking to mass update all contacts attached to a account. I do fetch all contacts using inbound SOQL query which do handles bulk processing too.
 
Now when it comes to update all contacts of all accounts which came into trigger, I do create a array and send tht entire array to update call which exceeds the limit of 100.
 
Is there any querymore or .. any other approach to be used .. pls let me know in detail on how to handle 100+ updates in Apex Trigger.
 
Thanks in advance
yogesh.rankawatyogesh.rankawat
Its a very bad limitation of salesforce that we can't update or insert more than 100 records at a time in triggers.
The alternate solution to overcome this problem is to create a time based workflow and execute it after every hour and each time update or insert 100 rows.
:smileyhappy:
PerGeertPerGeert

I do something like this:

Code:
// When Account address is changed, change address on contacts having the flag
// Address Same As Accounts equal true

trigger tgrAccountAddress on Account (after update) {

   // Create map of updated accounts
   Map<Id,Account> acctsWithNewAddress = new Map<Id,Account>();
   
   // Check if address was updated
   for (Integer i = 0; i < Trigger.new.size(); i++) {
    if (Trigger.old[i].PrimaryStreet__c != Trigger.new[i].PrimaryStreet__c ||
        Trigger.old[i].PrimaryCity__c != Trigger.new[i].PrimaryCity__c ||
        Trigger.old[i].PrimaryState__c != Trigger.new[i].PrimaryState__c ||
        Trigger.old[i].PrimaryPostalCode__c != Trigger.new[i].PrimaryPostalCode__c ||
        Trigger.old[i].PrimaryCountry__c != Trigger.new[i].PrimaryCountry__c ) {
        acctsWithNewAddress.put(Trigger.old[i].id,Trigger.new[i]);
    }
   }
   
   // Create list of Contacts to update
   List<Contact> updContact = new List<Contact>();
   Integer counter = 0;
   
   for (Contact co : [Select id, AccountId, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry From Contact
                               Where AccountId in :acctsWithNewAddress.keyset()
                               AND AddressSameAsAccounts__c = TRUE]) {
      Account acc = acctsWithNewAddress.get(co.AccountId);
      co.MailingStreet = acc.PrimaryStreet__c;
      co.MailingCity = acc.PrimaryCity__c;
      co.MailingState = acc.PrimaryState__c;
      co.MailingPostalCode = acc.PrimaryPostalCode__c;
      co.MailingCountry = acc.PrimaryCountry__c;
      updContact.add(co);
      counter++;
      
      if (counter == 1000) {
        update updContact;
        updContact.clear();
        counter = 0;
      }
   }
   //do final update
   if (counter > 0) update updContact;
}


 

Chirag MehtaChirag Mehta
Is this thing working fine ? Is It not throwing any Too Many Updates error ?

Doesn't the context of trigger remains the same when u call the update statement again for 100-200 records. Its like from same trigger context you are updating 100+ record.

Just wanted to confirm whether this approach works or not !!
PerGeertPerGeert
Works for me - could you post your code to see what the problem might be?
Chirag MehtaChirag Mehta
Ok Thanks !

I just wanted to confirm!!

There is no problem as such for me as of now, Anyhow can you confirm on number whether it is thousand or hundred records in one go ? Isnt it 100 updates in a single shot !!


Message Edited by Chirag Mehta on 01-10-2009 10:52 PM