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
fredkafredka 

Too many DML statments:21

I am getting the "too many dml statements" error when I do a bulk update.  I realize that the problem is that I have an insert statement inside a for loop but I am unsure of how to resolve the issue.  How do I get the fields I want to update into a map or list so that I can then update them all at once?  I am confused about the syntax...Here is the portion of my code that is giving me a problem... thanks!!!!!

///Part 2 B update //if health, update the health market if (HealthGo) { for(Account a: accts){ a.Health_Market__c=Market; a.Health_Renewal_Date__c=RenewalDate; a.Health_Underwriter_Assigned__c = Underwriter; update accts; } } else{ } //if dental, update the dental market if (DentalGo) { for(Account a: accts){ a.Dental_Market__c=Market; a.Dental_Renewal_Date__c=RenewalDate; update accts; } }

 

Best Answer chosen by Admin (Salesforce Developers) 
AnshulVermaAnshulVerma
for(Account a: accts){
if (HealthGo) {
a.Health_Market__c=Market;
a.Health_Renewal_Date__c=RenewalDate;
a.Health_Underwriter_Assigned__c = Underwriter;
}

else{
if (DentalGo) {
a.Dental_Market__c=Market;
a.Dental_Renewal_Date__c=RenewalDate;
}

}
} update accts; // remove the update from loop; update all the records alltogether.

All Answers

splitsplit

for(Account a: accts){ if (HealthGo) { a.Health_Market__c=Market; a.Health_Renewal_Date__c=RenewalDate; a.Health_Underwriter_Assigned__c = Underwriter; } else{ if (DentalGo) { a.Dental_Market__c=Market; a.Dental_Renewal_Date__c=RenewalDate; } } update accts; }

 

Message Edited by split on 02-08-2010 09:02 PM
AnshulVermaAnshulVerma
for(Account a: accts){
if (HealthGo) {
a.Health_Market__c=Market;
a.Health_Renewal_Date__c=RenewalDate;
a.Health_Underwriter_Assigned__c = Underwriter;
}

else{
if (DentalGo) {
a.Dental_Market__c=Market;
a.Dental_Renewal_Date__c=RenewalDate;
}

}
} update accts; // remove the update from loop; update all the records alltogether.
This was selected as the best answer
fredkafredka

Are you sure that will update all of the accounts?  I thought the for loop would allow me to "loop" through... I would think your proposed solution would only update the last one... ?

 

 

Thanks!

splitsplit

http://wiki.developerforce.com/index.php/Statements#Loops

it been my problem to:smileyvery-happy: