You need to sign in to do that
Don't have an account?
bh
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.
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
}