function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC New learnerSFDC New learner 

Need help with this issue


Hi All,
 I am trying to write a trigger which fires on Cases Object based on certain criteria on Opportunity .Once Case field is updated ,I am trying to send an email to that contact and also trying to insert two records with status as 'Sent' and another one with status as 'Scheduled'.
I have used Custome setting object  and a Class as a Custom object .
Below is my code.Can anyone help me on this issue and also can give me better solution.
trigger CaseEmailNotificationTrigger on Case (after update) {
   Map<ID,Case> addemailtemplate = new Map<ID,Case>();
   set<String> setEmailTemplateName = new set<String>();
   CaseEmailNotificationhandler handler = new CaseEmailNotificationhandler();
   if(Trigger.isUpdate){
       
       
   /*Fetch the Custom Setting and keep in the Map*/    
    Map <String , CaseEN__c > MapEmailTemplate  = new Map<String , CaseEN__c>();
  List<CaseEN__c> CaseENObject= [select Id, Name, Email_Code__c,Email_Template_Id__c,ScheduledEmail__c  from CaseEN__c];
       
   For (CaseEN__c CEN :CaseENObject )
   {
       MapEmailTemplate.put(CEN.Name , CEN);
   }
       
           
    List <EmailNotifications> EmailList = New List <EmailNotifications>();   
    EmailNotifications e;    
       
   for(Case cs : Trigger.New){
       system.debug('emailtemplate'+cs.Email_Template__c);
       if(cs.Email_Template__c != NUll){
           
           e = new EmailNotifications();
           e.CaseId= cs.id;
           e.EmailName = cs.Email_Template__c;
           e.EmailTemplateName = MapEmailTemplate.get(cs.Email_Template__c).Email_Template_Id__c;
           e.ScheduledFlag=  (boolean) MapEmailTemplate.get(cs.Email_Template__c).ScheduledEmail__c;
           e.ParentEmail = cs.Email_Template__c;
           e.OpporunityId = cs.Opportunity__c;
           e.EngagementId = cs.Engagement_ID__c;
           e.SessionId = cs.Session_ID__c;
           EmailList.add(e);            
       }
       
   }
       if(EmailList.size()>0){
           //CaseEN handler = new CaseEN();
          // handler.sendEmail(EmailList);
           sendEmail(EmailList);
       }
       
public void sendEmail(list<EmailNotifications> Emaillist){
       List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
       
        List<OrgWideEmailAddress> owea = [select Id from OrgWideEmailAddress where Address =: System.Label.Feed_From_Address];
       Map<Id, Id> map_Formfields = new Map<Id, Id>();
       for(EmailNotifications en:Emaillist){
           case cas = en.CaseId;
           
           Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
           String[] toAddresses = new String[] {'sirisha.yada@gmail.com'};
   system.debug('toAddresses' +toAddresses);
           mail.setToAddresses(toAddresses);
           mail.setSubject('Hi');
           system.debug('hi');
           mail.setSenderDisplayName('Siri');
           system.debug('siri');
         
          String body = 'Dear ' + cas.ContactEmail + ', ';
           body += 'First mail';
           system.debug('body'+body);
           mail.setHtmlBody(body);
           mail.setWhatId(cas.Id);
           mails.add(mail);
           
           list<Email_Notification__c> notificationlist = new list<Email_Notification__c>();
           List<Email_Notification__c> reminderNotification = new List<Email_Notification__c>();
           List<Email_Notification__c> updatereminderNotification = new List<Email_Notification__c>();
           Email_Notification__c em;
           if(en.ScheduledEmail__c == False){
             em.Opportunity__c = en.OpporunityId;
             em.Case__c = en.CaseId;
             em.Status__c = 'Sent';
             em.Sent_Date__c = System.today();
             em.Immediate__c = True;
             em.Scheduled_Date__c = System.today();
             em.Email_Template_Name__c = en.EmailTemplateName;
             em.Name = en.EmailName;
             em.Email_Template__c = en.EmailName;
               notificationlist.add(em);    
   }
           if(en.ScheduledEmail__c == True){
               Email_Notification__c updatelist = new Email_Notification__c();
               
               updatelist = [select id,status__c,Sent_Date__c from Email_Notification__c where   Opportunity__c = en.OpporunityId and Email_Template_Name__c = en.EmailTemplateName and Status__c = 'Scheduled'];
                   updatelist.Sent_Date__c =system.today();
               updatereminderNotification.add(updatelist);
               
           }
           if(en.ParentEmail == em.Email_Template_Name__c ){
                 
                     Email_Notification__c em1 = new Email_Notification__c();
                     em1.Name = emailtemplatename;
                     em1.Status__c = 'Scheduled';
                     system.debug('em1'+em1.Status__c);
                     em1.Immediate__c = false;
                     system.debug('em1'+em1.Immediate__c);
                     em1.Scheduled_Date__c = System.today() + 7;
                     system.debug('em1'+em1.Scheduled_Date__c);
                     em1.Email_Template_Name__c = etId;
                     system.debug('em1'+em1.Email_Template_Name__c);
                     em.Email_Template__c = emailtemplatename;
                     system.debug('em1'+em1.Email_Template__c);
            reminderNotification.add(em1);
           
              
         }
   }
   if(notificationlist.size()>0){
                   system.debug('size'+notificationlist.size());
                       
                   insert notificationlist;
   }
   if(updatereminderNotification.size()>0){
       system.debug('size of ren'+updatereminderNotification.size());
       Update updatereminderNotification;
   }
       if(reminderNotification.size()>0){
                   system.debug('size'+reminderNotification.size());
                       
                   insert reminderNotification;
   }
   }     
   
   
   
   }
   
}      



I really appreciate for your help.
Thanks,
Sirisha