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
bhbh 

Inserting Many (200+) records

Hi All,

 

I'm developing code to send messages to all contacts in, this is currently around 800 Contacts. after sending the message,  I would like to create a Completed Task which denotes the transaction.

 

My problem is that Salesforce limits to 200 Inserts per call (please correct me if I'm wrong).


Could someone please refer me to hints on how I would develop this kind of code?

 

Thanks so much.

 

 

BodhiDharmaBodhiDharma

You can batch your logic in a SOQL for loop like this:

 

 


List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

//This syntax will retrieve 200 records at a time and process any logic

//you have in the loop for that batch
for(List<Contact> cons : [select Id, email from Contact where email != null]){
for(Contact con : cons){
//Set up an email message to send
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setHtmlBody('Test');
//Must set target object ID to use setSaveAsActivity
mail.setTargetObjectId(con.Id);
//Save an activity for the email that was sent
mail.setSaveAsActivity(true);
mails.add(mail);
}
//Send it here
Messaging.sendEmail(mails);
//Also process any DML here (not in the previous for loop)

//it will execute for the batch of Contacts

}

 

 

 

Message Edited by BodhiDharma on 08-10-2009 02:24 PM