+ Start a Discussion
John C SchaafJohn C Schaaf 

If an Apex job designed to deliver emails in a queue to an email contact is not delivering the first email, will subsequent emails to that contact be delivered?

I didn't write the below code, and am very new to understanding it.

I have a situation where an Apex job is designed to deliver multiple emails to an email contact at 5, 10, 15, and 30 days after the contact has been created. If the first email at 5 days post-contact creation is not sent, does that mean that the emails that are supposed to be sent at 10, 15, and 30 days will also not be sent by that same Apex job?


global class contact_Malpractice_SchedulableClass implements Schedulable {
     global void execute(SchedulableContext ctx) {
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :ctx.getTriggerId()];
      System.debug(ct.CronExpression);
      System.debug(ct.TimesTriggered); 
    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'network@example.com'];
      List<EmailTemplate> etList=[SELECT id,name FROM EmailTemplate WHERE name like 'Providers: Malpractice%'];
      Map<Integer,Id> etMap=new Map<Integer, Id>();
      for(EmailTemplate et:etList){
        if(et.name=='Providers: Malpractice Expires in 0 Days (Today)'){
          etMap.put(0,et.id);
        }else if(et.name=='Providers: Malpractice Expires in 15 Days'){
          etMap.put(15,et.id);
        }else if(et.name=='Providers: Malpractice Expires in 30 Days'){
          etMap.put(30,et.id);
        }else if(et.name=='Providers: Malpractice Expires in 45 Days'){
          etMap.put(45,et.id);
        }else if(et.name=='Providers: Malpractice Expires in 60 Days'){
          etMap.put(60,et.id);
        }else if(et.name=='Providers: Malpractice Past Due 15 Days'){
          etMap.put(-15,et.id);
        }else if(et.name=='Providers: Malpractice Past Due 30 Days'){
          etMap.put(-30,et.id);
        }else if(et.name=='Providers: Malpractice Past Due 45 Days'){
          etMap.put(-45,et.id);
        }else if(et.name=='Providers: Malpractice Past Due 60 Days - Account Deactivated'){
          etMap.put(-60,et.id);
        }
      }
    Id teamQueID=[select Id from Group where Name = 'Team Que' and Type = 'Queue'][0].Id;
    Id providerRelationQueID=[select Id from Group where Name = 'Provider Relations' and Type = 'Queue'][0].Id;
      List<Messaging.SingleEmailMessage> mails=new List<Messaging.SingleEmailMessage>();
      List <Case> caseList = new List<Case>();
    List<Contact> recipientList = [select id,TB_Profile_Display_State__c,AccountId,Malpractice_Expiration_Date_Days__c from contact where TB_Approval_State_on_Site__c='Active' and RecordType.Name='Provider Contact' and Malpractice_Expiration_Date__c!=null and Malpractice_Expiration_Date_Days__c IN (-90,-60,-45,-30,-15,0,15,30,45,60)];
    List<Contact> recipientUpdateList =new List<Contact>();
      for(Contact recipient:recipientList){
        Integer expirationDays=(Integer)recipient.Malpractice_Expiration_Date_Days__c;
        if((etMap.containsKey(expirationDays) && owea.size()>0)||expirationDays==-90){
          if(etMap.containsKey(expirationDays) && owea.size()>0){
            Id etID=etMap.get(expirationDays);
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          mail.setOrgWideEmailAddressId(owea.get(0).Id);
          mail.setTargetObjectId(recipient.id);
          mail.setTemplateId(etID);
          mails.add(mail);
          }
          if(expirationDays==0 || expirationDays==15 || expirationDays==-15 || expirationDays==-30 || expirationDays==-60 || expirationDays==-90){
              Case ca = new Case();
              ca.SlaStartDate=Datetime.now();
              ca.ContactId=recipient.Id;
              ca.AccountId=recipient.AccountId;
              ca.Status='Working (24 hr SLA)';
              ca.Origin='Scheduled Job';
              ca.Type='Malpractice';
              if(expirationDays==15){
                ca.OwnerId=teamQueID;
                ca.Subject='Call Provider: Malpractice (COI) expires in 15 days';
              }else if(expirationDays==0){
                ca.OwnerId=teamQueID;
                ca.Subject='Call Provider: COI Expires Today';
              }else if(expirationDays==-15){
                ca.OwnerId=teamQueID;
                ca.Subject='Call Provider: COI Expired 15 Days Ago';
              }else if(expirationDays==-30){
                ca.OwnerId=providerRelationQueID;
                ca.Subject='Call Provider: COI Expired 30 Days Ago';
              }else if(expirationDays==-60){
                ca.OwnerId=providerRelationQueID;
                ca.Subject='Call Provider: COI Expired 60 Days Ago';
                recipient.TB_Profile_Display_State__c='Hidden';
                recipientUpdateList.add(recipient);
              }else if(expirationDays==-90){
                ca.OwnerId=providerRelationQueID;
                ca.Subject='Inactivate Provider Due to 90 Past Due COI';
                recipient.TB_Profile_Display_State__c='Hidden';
                recipientUpdateList.add(recipient);
              }
              caseList.add(ca);
          }
        }
      }
      insert caseList;
      update recipientUpdateList;
    Messaging.sendEmail(mails);
    }
}