+ Start a Discussion
JT HargreavesJT Hargreaves 

On an EmailtoCase trigger, I'm able to get a handle on incoming attachments when AutoResponse is enabled but when it is removed, I no longer have access to attachments. What does the autoresponse do that allows access to the attachments? Any ideas?

trigger V_Forward on Case (after update) {
 for (case ca : Trigger.new){
    
        
     //Get Variables from Custom Settings
     V_E2CSettings__c vs = V_E2CSettings__c.getOrgDefaults();
     string email2CaseAddr = vs.Email2Case_Email_Address__c;
     string sharedMailboxEmail = vs.Shared_Mailbox_Email__c;
     string vitalystEmailAddr = vs.Vitalyst_Email_Address__c;
     string vitalystQueueId = vs.Vitalyst_Support_Queue_Id__c;
     
     //Only run on this if the owner is set to Vitalyst Queue
     if (ca.OwnerId == VitalystQueueId){
      string orgID = UserInfo.getOrganizationId();
      string org15 = orgID.left(15);
      string caseID = ca.id;
      string case15 = caseID.left(15);
      string rOrgID = org15.right(10);
      string rcaseID = case15.right(10);
      string fcaseID = rcaseID.left(5);
      
      //Establish the reference number
      string refI = 'ref:_' + org15.left(5) + rOrgID.replace('0','').left(5) + '._'+ case15.Left(5) + fcaseID.replace('0','') + case15.right(5) +':ref';   
      
      
      //Get a list of the email messages.  We want the 1st one so it should be emailmsgList[0]     
    
      List<EmailMessage> emailmsgList = [select id, parentId, Subject, TextBody, HtmlBody, FromAddress, CcAddress, BccAddress from EmailMessage where parentID =: ca.Id order by CreatedDate ASC];
         //Only run if it returns 1 email (the original)
         if (emailmsgList.size()==1){

            EmailMessage eM = emailmsgList[0];
            
            System.debug('emailMsglist size is '+ emailMsglist.size()); //JSR Debugger
            
            // Create a list to hold the two emails we'll send
            List<Messaging.SingleEmailMessage> twoEmails = new List<Messaging.SingleEmailMessage>();
             
          
            //*****EMAIL FOR SHARED MAILBOX******  
            //Create a new email message to send to shared mailbox
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            
            String attachmentNms = 'No Attachments '; 
            //Prepare body for response
            String nameContact = ca.SuppliedName;
            
             if (nameContact == null){
                 nameContact = 'Sir/Madam';
             }             
             
            //Set to addresses to customer AND sharedMailboxEmail
            String[] toAddresses = new String[] {sharedMailboxEmail};
                     toAddresses.add(eM.FromAddress);
                     //Add the email2case address so the message is added to the case emails
                     toAddresses.add(email2CaseAddr); 

             mail.setToAddresses(toAddresses);   
             if (eM.CcAddress !=null){
                  String[] ccAddresses = new String[] {eM.CcAddress};
                  mail.setCcAddresses(ccAddresses);     
             }
            
            mail.setReplyTo(email2CaseAddr); 
            mail.setSenderDisplayName('Support');
            mail.setSubject('Your case has been received : ' + eM.Subject + ' '+ refI);
            mail.setPlainTextBody(eM.TextBody);
            mail.setHtmlBody('Email Id: '+ eM.id + ' Dear ' + nameContact + ', 

Thank you for contacting Support.  Case#: ' + ca.CaseNumber + ' has been created and a support representative will respond shortly.

We are committed to customer satisfaction and will do our best effort to respond the same business day.

<b> -Support</b>

'+ 'BT Reference: ' + refI + '

' + eM.HtmlBody );
           // mail.setHtmlBody('Customer Email: ' + eM.FromAddress + '
' + 'ORG_ID: [ssf39E69]' + '
'+ 'Case#: ' + ca.CaseNumber + '
' + 'BT Reference: ' + refI + '

' + eM.HtmlBody ); 
                 
            //Loop through the attachments and add them to the email
            //***** IT IS NOT FINDING ATTACHMENTS HERE AT ALL*****

            List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            for (Attachment a : [select id, Name, Body, BodyLength from Attachment where ParentId = :eM.Id]){
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setFileName(a.Name);
                efa.setBody(a.Body);
                fileAttachments.add(efa);
                System.debug('attachment file name is '+ a.Name); //JSR Debugger
            	
                //attachmentNms = a.name + ', ' + attachmentNms; // + ', ' + a.name;
                                
                /* Remove cMail
                //Add these attachments to the Case Email message too
                Attachment att = new Attachment();
                att.Name = a.Name;
                att.body = a.body;
                att.parentId = cMail.Id;       
                insert att;
                */
             }
            
            mail.setSubject('Your case has been received : ' + eM.Subject + ' '+ refI + ' ' + attachmentNms);
            mail.setFileAttachments(fileAttachments);      
            
            //Add this email to the list of emails to send
            twoEmails.add(mail);
      
            //*****EMAIL FOR THEIR TICKETING SYSTEM****** 
            //Create a new email message to send to  their ticketing system - No Attachments or Body from original email
            Messaging.SingleEmailMessage vMail = new Messaging.SingleEmailMessage();

            //Set to addresses to Vitalyst Email
             String[] toVAddresses = new String[] {vitalystEmailAddr}; 
            vMail.setToAddresses(toVAddresses);
            vMail.setReplyTo('noreply@binarytree.com'); 
            vMail.setSenderDisplayName('Support');
            vMail.setSubject('New case received: ' + eM.Subject + ' '+ refI);
            vMail.setPlainTextBody('ORG_ID: [adsf]' + '
' + 'Customer Email: [' + eM.FromAddress + ']
' +  + '
'+ 'Case#: ' + ca.CaseNumber + '
' + 'BT Reference: ' + refI );
            vMail.setHtmlBody('ORG_ID: [4AA2839E69]' + '
'+'Customer Email: [' + eM.FromAddress + ']
' + 'Case#: ' + ca.CaseNumber + '
' + 'BT Reference: ' + refI); //JSR - eM.FromAddress - We need the customer address in the email so Vitalyst can reply to customer
            
            //Add this email to the list of emails to send
            twoEmails.add(vMail);
       
            //Send both emails
            List<Messaging.Sendemailresult> result = Messaging.sendEmail(twoEmails, true);
            
            //For debugging on SendEmail failure
            if(result[0].isSuccess() == false) {
                List<Messaging.Sendemailerror> error = result[0].getErrors();
                system.debug('Vitalyst SendEmailInfo: ' + error[0].getFields() + ' / ' + error[0].getTargetObjectId() + ' / ' + error[0].getMessage());
            }
          }
       }
    }
}