+ Start a Discussion
Timothy SmithTimothy Smith 

Trigger, after first SOQL seems to stop executing without error.

When I run my test class the first few lines will execute but then the logs seem to show it stops executing for no reason.  I get the first two system.debugs so I know it is partially executing.

This Trigger will send an email message if an Account has 8 or more cases created within 7 days. Depending on the status of the four fields in the If/Else block will dictate the destination of the email. If the status is "Active" 1 email is sent to a set email address. If the status is "Implementation" then 2 emails are sent, addresses are grabbed from SOQL.
Any help is appreciated.
 
trigger CaseCountAlertTrigger on Case (before insert) {
List<String> emailAdds = new List<String>();                            // Holds '2' ToAddresses from Milestone1_project__c object
Set <Id> AcctIds = new Set <Id>();                                      //Holds Account Ids from this Case Trigger  
String messageToSend;                                                   //Email body sent in email (will be in HTML format)
Map < Id, String > accountIdEmailmessageMap = new Map < Id, String > (); // map of AccountId and Email body per Account/AccountId to be sent

List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                                    FROM Case
                                                    WHERE CreatedDate = LAST_N_DAYS:7 AND Id in :Trigger.New 
                                                    GROUP BY Account.Name, AccountId
                                                    HAVING COUNT(Id) >= 8
                                                    ];

System.debug('AggregateResult:  ' + AggregateResultList);  // debugs:    'AggregateResult: ()'  --emtpy aggregate list
  System.debug('Trigger Results' + Trigger.new);           // This line will print the Case that comes in as Trigger.New, debugs as expected.

//  ******** It seems to exit here***************

for (AggregateResult aggr: AggregateResultList){
                       messageToSend = 'You are receiving this email alert due to an account ';
                       messageToSend += 'activity rule has exceeded 8 cases created within 5 business days.<br><br>';
                       messageToSend += 'Please, follow up with the account and provide guidance and assistance.<br><br>';
                       messageToSend += '<b>Account Name:  </b>' + aggr.get('name') + '<br> <br>';
                       messageToSend +=  'Thank you, <br>';
                       messageToSend +=  'Salesforce Team';

             //Crete Map of <AccountId, Message to serve as body in Email
             //                         for each accountId>       
                Id accId = (Id) aggr.get('AccountId');
                accountIdEmailmessageMap.put(accId, messageToSend);

            //Create List of AccountId's to grab email addresses
            // from child Object for 'Implementation Status 
                AcctIds.add(accId);  

         }

                System.debug(accountIdEmailmessageMap);  


    /*
    List < Case > caseList = [SELECT Id, AccountId, Account.Name, Account.Eyefinity_EHR_Status__c,
                              Account.Eyefinity_PM_Status__c, Account.OfficeMate_Status__c,
                              Account.Project_Imp_Status__c                
                              FROM Case
                              WHERE AccountId IN: AcctIds];
    */


    // SOQL to grab the four status fields on Account to check status either 'Active' or 'Implementation'
    // also grab two email addresses for use in ifElse block
    List<Account> accList = [SELECT Id, Name, Eyefinity_EHR_Status__c, Eyefinity_PM_Status__c,
                                    Project_Imp_Status__c, OfficeMate_Status__c,(select Client_Advisor_Email__c,
                                                                                 Resource_Coordinator_Email__c
                                                                                 from Projects__r) 

                             FROM Account
                             WHERE Id IN :AcctIds];

    List<Messaging.SingleEmailMessage> lstASingleEmailMessage = new List<Messaging.SingleEmailMessage>();
    List<Messaging.SingleEmailMessage> lstBSingleEmailMessage = new List<Messaging.SingleEmailMessage>();


    for (Account al: accList) {

        if (al.Eyefinity_EHR_Status__c == 'Active' ||
            al.Eyefinity_PM_Status__c == 'Active' ||
            al.Project_Imp_Status__c == 'Active'  ||
            al.OfficeMate_Status__c == 'Active') {

               // String messageBody = accountIdEmailmessageMap.get(al.accId);

                //Send Email to CustomerService if Active
                List<String> emailaddr = new List<String>();
                emailaddr.add('CustomerSuccessManagers@test.com');  

                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setSenderDisplayName(' Support');
                mail.setToAddresses(emailaddr);   
                mail.Subject = 'Notification:  Account Case activity rule exceeded';
                mail.setHtmlBody(messageToSend);
                lstASingleEmailMessage.add(mail);
            }


            else if (al.Eyefinity_EHR_Status__c == 'Implementation' ||
                     al.Eyefinity_PM_Status__c == 'Implementation' ||
                     al.Project_Imp_Status__c == 'Implementation' ||
                     al.OfficeMate_Status__c == 'Implementation'){
                String messageBody1 = accountIdEmailmessageMap.get(cl.AccountId);        

                System.debug('Accounts: ' + al);

               //Send email to Coordinator and Advisor if in Implementation

                for(Account a : accList)
                {
                    for(Milestone1_Project__c p : a.Projects__r)
                    {   
                        emailAdds.add(p.Client_Advisor_Email__c);
                        emailAdds.add(p.Resource_Coordinator_Email__c);
                    }
                }

                System.debug(emailAdds);
                Messaging.SingleEmailMessage amail = new Messaging.SingleEmailMessage();
                amail.SetSenderDisplayName('Support');
                amail.setToAddresses(emailAdds);
                amail.Subject = 'Notification:  Account Case activity rule exceeded';
                amail.setHtmlBody(messageBody1);
                lstASingleEmailMessage.add(amail); 

            }  
            else{
                System.debug(AggregateResultList);

            }
    }


    Messaging.SendEmailResult[] r = Messaging.sendEmail(lstASingleEmailMessage); 

}

Test Class:
 
@isTest
private class TestCaseHandlerAlert {

  @testSetup static void setup(){
      List<Account> testAccounts = new List<Account>();

        Account a = new Account();
            a.name = 'AccountEHRImplement';
            a.RecordTypeId = '01230000000v58OAAQ';
            a.Eyefinity_EHR_Status__c = 'Implementation';
            a.Tax_Id__c = '123456789';
      testAccounts.add(a);

        //Insert Account
            insert testAccounts;

         //Create User:  THis user will fill required fields on the Milestone1_Project1 allowing for a an email address to populate
        User tuser = new User(  firstname = 'tuserFname',
                            lastName = 'tuserLastname',
                            email = 'test@tester.com',
                            Username = 'tuserleielkwl@test18278391.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias ='Blah',
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId =[Select Id From Profile Where Name='Eyefinity Managers'].id
                           );  
        insert tuser;

      //Create Project
        Milestone1_Project__c project1 = new Milestone1_Project__c();
            project1.Customer_Account__c = [Select Id FROM Account Where Name ='AccountEHRImplement'].id;
            project1.Name = 'triggerProject';
            project1.Client_Advisor__c = [Select Id FROM User Where Username ='tuserleielkwl@test18278391.org'].id;
            project1.Resource_Coordinator__c = [Select Id FROM User Where Username ='tuserleielkwl@test18278391.org'].id;  
            project1.RecordTypeId = '01214000001RYp7AAG';    
                insert project1;
    }

    @isTest static void AccountEHRImplement (){

          //Create and insert more than more than 8 cases  
            List<Case> casestoInsert = new List<Case>();     
        for (Integer i=1; i<10; i++){
            Case cas1 = new Case();
                cas1.RecordTypeId = '01214000001NcOYAA0';
                cas1.AccountId = [Select Id FROM Account Where name ='AccountEHRImplement'].id;
                cas1.Origin = 'Phone';
                cas1.Impact__c = 'Low';
                cas1.Severity__c = 'Minor';
                cas1.Type = 'Bridge';
            casesToInsert.add(cas1);
         }

        Test.startTest();
            insert casesToInsert; 
        Test.stopTest();  

        System.debug('Expected: 2, actual: ' + Limits.getEmailInvocations());  
    }
}

User-added image​​​​​​​
Timothy SmithTimothy Smith
I copied/pasted renamed trigger and test class.  issue resolved.  I have no idea why