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 to capture multiple records in trigger

Hi All,
I am just a beginner learning how to write bulkified code.
I have created a Batch which updates a field in Case and then updates the Status in my Custom Object.
Before creating Batch, I am creating two records with Status as "Sent" and another one "Scheduled".
My Batch should run daily and updates the case and sends out the email.



So basically 4 things happen in my code.
1) Through Process Builder , I am updating the case picklist field to say 'XX' .
2) When Case trigger fires , It needs to Insert two records in my Custom object  with one status as 'Sent' and  another one as  'Scheduled'.
3) Batch runs daily and checks for the Scheduled records on that day and updates the case field picklist as say 'YY'.
4) Once the case field is updated to 'YY' ,sends out email and updates my Custom object Status as "Sent".

I can able to update the case and send the email once. But when multiple records updates the case for multiple opportunities then my code is fetching only one record.
I am capturing those cases from my Custom Object whose status is Scheduled and kept them in a list and iterating on those list of cases, contactid and templateid sending out the email.
Once the email is success ,I am trying to insert two records.So, I am facing problem to get the caseid from the list of cases while sending the email.
If I use map ,it is capturing one case and  If I use list I am not able to get the multiple cases.

This is code for capturing all the cases 
for(Case cs : newList){
                system.debug('inside case'+cs);
              
                ListEmailtemplateCase.add(cs.Email_Template__c);
                system.debug('cs email template @@'+cs.Email_Template__c);
                
            }
This code will get the developername from the Emailtemplate object for those cases
for(EmailTemplate et:[Select id,DeveloperName,Name,body from EmailTemplate where DeveloperName IN: ListDeveloperName]){
                system.debug('et'+et.Name);
                system.debug('et Id'+et.Id);
             
              EmailTemplateList.add(et.DeveloperName);
                EmailTemplateId.add(et.Id);
          }
This code to capture the template id and cases sends the email to that contact.
integer i=0;
         for(Case c: newList)
            {
                system.debug('newlist size***'+newList.size());
                try{  
                if(c.Email_Template__c != NULL){ 
                    system.debug('inside case emailtemplate');
                system.debug('email template'+c.Email_Template__c);
                    Id templateId = EmailTemplateId[i];
                system.debug('templateid'+templateId);
                
            //caseLists.add(c.Id);
                
    Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
            mail.setTemplateId(templateId);
            
                if(c.ContactId != NULL){
                    system.debug('contactid'+c.ContactId);
            mail.setTargetObjectId(c.ContactId);
                }
            mail.setWhatId(c.Id);
            
             if ( owea.size() > 0 ) {
                 system.debug('owea size'+owea.size());
                mail.setOrgWideEmailAddressId(owea[0].Id);
            }
                
            mails.add(mail);
            
            List<Id> caseIdlist = new List<Id>();
            List<Case> caselist = new List<Case>();
                system.debug('sendemail'+Messaging.sendEmail(mails));
            List<Messaging.SendEmailResult> list_mr =Messaging.sendEmail(mails);
             Integer j=0;
            for(Messaging.SendEmailResult ser : list_mr){
               Map<Id,Case> casemap = new Map<Id,Case>();
                system.debug('inside messaging email result');
                if(ser.isSuccess()){
                    system.debug('inside if');
                    Messaging.SingleEmailMessage maill = mails[j];
                    Id caseId = maill.getwhatId();
                    system.debug('caseId'+caseId);
                    caseIdlist.add(caseId);
                    system.debug('caseIdlist'+caseIdlist);
                    system.debug('caseIdlist'+caseIdlist);
                    Case cas = casemap.get(caseId);
                    system.debug('case'+cas);
                    caselist.add(cas);
                    system.debug('caselist'+caselist);
                    system.debug('caselist'+caselist.size());
                    createEmailNotifications(caselist);
                    
                }
                }
                    j++;
            }
            }
This line  Case cas = casemap.get(caseId) is not getting all those list of cases.
Please help me with this functionality. Any help would be greatly appreciated. 

Thanks