1

I want to insert multiple cases, which will then send out emails for each case. My EmailMessage Trigger fires for each single email but I want it to only run once.

The method which triggers the Case Insert

public with sharing class createCases {

  @AuraEnabled
  public static Boolean createCases(List<Case> listofCases) {
      List<Case> casesToInsert = new List<Case>();

      for (Case c : listofCases) {
          casesToInsert.add(new Case(Subject = 'test'));
      }
    
      if (!casesToInsert.isEmpty()) {
          insert casesToInsert;
          return true;
      }
  }
}

This method is in my isAfter && isInsert case trigger

    public static void newCases(List<Case> cases) {
      List<Case> myCases = new List<Case>();
      List<Messaging.SendEmailResult> emailGroup = new List<Messaging.SendEmailResult>();
      List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

      for (Case c : myCases) {
          myCases.add(c);
      }

      myCases = new List<Case>([SELECT Id, ContactId FROM Case WHERE Id IN :myCases]);

      for (Case c : myCases) {
        
          Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

          email.setTargetObjectId(c.ContactId);
          email.setWhatId(c.Id);
          email.setSaveAsActivity(true);
          email.setUseSignature(false);
          email.setTemplateId(myEmailTemplate.Id);
          email.setOrgWideEmailAddressId(myEmailAddress.Id);
                 
          emails.add(email);
      }

      emailGroup.addAll(Messaging.sendEmail(emails));
  }

EmailMessageTrigger

trigger EmailMessageTrigger on EmailMessage (before insert, before update, after insert, after update) { 

  System.debug('Trigger.new: ' + Trigger.new);
}

If I have 50 cases, this fires for each single email, but I only want it to fire once. Basically, I want Trigger.New to contain a List of all the emails, but instead it only contains one.

How should I re-write my newCases method to send out a list of emails? Thanks in advance

1 Answer 1

0

Your method needs to be modified like below.

public static void newCases(List<Case> cases) {
//The emails list variable is used to store the list of emails to send notifications
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
//Query the email tempate id using below query
EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE DeveloperName =:emailTemplateName];
//Iterate the cases list variable
for(Case cs:cases){

     If(cs.ContactId !=Null && cs.SupliedEmail !=Null){

        //toAddress list variable is used to store the list email addresses that we need to send
        List<string> toAddress = new List<string>();
        toAddress.add(cs.SupliedEmail);
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setTemplateId(et.Id);
            email.setToAddresses(toAddress);
            email.setTargetObjectId(cs.contactId);
            email.setWhatId(cs.contactId);
            email.setSaveAsActivity(true);
            email.setUseSignature(false);

        emails.add(email);
        }
  }
// this is used to send an email
 Messaging.sendEmail(emails);

}
3
  • Same result. Messaging.sendEmail(emails) still makes the trigger fire for each email record :( Commented Dec 17, 2022 at 16:35
  • Whenever case is created then email will trigger Commented Dec 17, 2022 at 16:38
  • What I'm specifically working on is inserting multiple cases at the one time. I want to put a query in my EmailMessage trigger, however, it's giving me a "too many SOQL queries" error because if I insert 50 cases, the EmailMessage trigger is being ran 50 times. Commented Dec 17, 2022 at 16:43

You must log in to answer this question.

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