1

I have apex class which is sending email and it works perfect, but together with email I want to change Case status and update a custom field. Inside for loop it works, but I don't want to exceed governor limits. Can you please take a look?

PS. If-else statement with ContactEmail is only example, because I will change that in different DEV environment

public class EmailClass
{
    @InvocableMethod
    public static void sendEmail() {
    List<Contact> contacts =[Select Id From Contact where Email!=null];
    List<Case> cases = new List<Case>();
    cases = [Select Id,ContactEmail, ContactId From Case Where Follow_Up_Date_in_Days__c=6 AND ContactEmail!=null AND status='New' AND ContactId IN : contacts];
    OrgWideEmailAddress owea = new OrgWideEmailAddress();
    owea = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='myCompany'];
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Case cs : cases)
    {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        // set properties on singular email
        List<String> toAddress = new List<String>();
        toAddress.add(cs.ContactEmail);
        email.setToAddresses(toAddress);
        email.setReplyTo('[email protected]');
        email.setWhatId(cs.Id);
        email.setTargetObjectId(cs.ContactId);
        email.setOrgWideEmailAddressId(owea.id);
        List<String> ccAddress = new List<String>();
        email.setCcAddresses(ccAddress);
        //email.setSaveAsActivity(true); 
        cs.Letter_Sent__c = true;
        cs.Status = 'In Progress'
        if (cs.ContactEmail=='[email protected]')
        {
            email.setTemplateId('00X5t000000UJX5EAO');
        }
        else
        {
            email.setTemplateId('00X5t000000IA6jEAG');
        }
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); 
        update cs;
1
  • 2
    Add each email to a list Messaging.SingleEmailMessage[] emails and send emails in one go after the loop. Equivalently, update cases after the loop instead of updating each case individually inside. Sep 2, 2022 at 13:34

1 Answer 1

1

As you already created List<Messaging.SingleEmailMessage> emails list in your code ,
You need to add each email to this list and send the emails after the loop.
As well you should update the cases list after the loop.

So the updated code will look like this :

public class EmailClass {

    @InvocableMethod
    public static void sendEmail() {
        // be careful with this query as you can have more than 50 000 Contacts where Email!=null 
        List<Contact> contacts =[Select Id From Contact where Email!=null];
        List<Case> cases = new List<Case>();
        cases = [Select Id,ContactEmail, ContactId From Case Where Follow_Up_Date_in_Days__c=6 AND ContactEmail!=null AND status='New' AND ContactId IN : contacts];
        OrgWideEmailAddress owea = new OrgWideEmailAddress();
        owea = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='myCompany' LIMIT 1];
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        for (Case cs : cases)  {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            // set properties on singular email
            email.setToAddresses( new List<String>{ cs.ContactEmail } );
            email.setReplyTo('[email protected]'); 
            email.setWhatId(cs.Id);
            email.setTargetObjectId(cs.ContactId);
            email.setOrgWideEmailAddressId(owea.id);
            email.setCcAddresses( new List<String>() );
            //email.setSaveAsActivity(true); 
            cs.Letter_Sent__c = true;
            cs.Status = 'In Progress'
            if (cs.ContactEmail=='[email protected]') {
                email.setTemplateId('00X5t000000UJX5EAO');
            } else {
                email.setTemplateId('00X5t000000IA6jEAG');
            }
            emails.add( email ); // here we add the email to the list
        }
        Messaging.sendEmail( emails ); // here we send all emails 
        update cases; // here we update cases list
    }
}
2
  • Thank you so much! :) It works perfectly, now I can see how to add it to the list! :)
    – NewToApex
    Sep 6, 2022 at 11:38
  • Great to hear. Please accept this answer as the one which resolved your issue and you could upvote it as well! :) Sep 6, 2022 at 11:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .