+ Start a Discussion
Scott RussoScott Russo 

Forward the first Support Case Email

We are looking for a way to forward the initial email related to a case, to our shared mailbox, outside of Salesforce.  Is there a way to forward that email, it's contents(including attachments) using a workflow trigger or some other automated process in Salesforce?
The email being forwarded needs to include the Case Ref ID in the subject, and have any attachments which existed in the incoming email.
trigger emauilwithatt on Case (before insert,before update) {
    for (case ca : Trigger.New){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'XXXXX@gmail.com'};
            String[] ccAddresses = new String[] {'XXXXX@gmail.com'};
        mail.setSubject('your case number is'+ca.CaseNumber);
        mail.setPlainTextBody('This is case  email body.');
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :ca.Id]){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Scott RussoScott Russo
Thank you for the quick reply. We will test the solution you provided, and hopefly it gives us what we need.  I will reply back when testing is complete.  Thanks again
Scott RussoScott Russo
We implimented the above code, and modified the email addresses.  The trigger responded to the newly created support case, and sent an email reply, but it did NOT include the original email body, nor the attachments.
It also is sending a reply for every case update. So when the case is updated, a notification is being sent to our shared mailbox.
Is this working for you?
Kristi ColemanKristi Coleman
Hi Scott,
Did you find a solution to this issue?  If so, would you mind sharing it?  We have nearly the same use case.  Thanks.
Scott RussoScott Russo
Hi Kristi,  Yes we did get it to work. My apologies for not updating this post.
In order for this to work where attachments are included in the forwarded email, you must have an autoresposne email go out first. For some reason we could not get this to work unless there were at least two emails related to the support case record.

Here is the apex trigger code. We used a  custom settings record so we could test in sandbox and production witthout having to change the code since we are checking which queue the case is in, and queue IDs are different between production and sandbox.
This code forwards to a shared mailbox, and the distribution list of the original email.
trigger Forward on Case (after update) {
 for (case ca : Trigger.new){
      //5/2018 - JT Hargraves
     //Try to run later after this field value has been set.
    System.Debug('The Email Trigger code is running');
     //Get Variables from Custom Settings
     V_E2CSettings__c vs = V_E2CSettings__c.getOrgDefaults();
     boolean enableTrigger = vs.Enabled__c;
     string email2CaseAddr = vs.Email2Case_Email_Address__c;
     string sharedMailboxEmail = vs.Shared_Mailbox_Email__c;
     string VEmailAddr = vs.Email_Address__c;
     string QueueId = vs.Support_Queue_Id__c;
     //First Check to see if the trigger should run
     if (enableTrigger == true) {
         //Only run on this if the case owner is set to a specific Queue
         if (ca.OwnerId == QueueId){
          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 2 email (the original)
             if (emailmsgList.size()==2){
                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();
                //Prepare body for response
                String nameContact = ca.SuppliedName;
                 if (nameContact == null){  nameContact = 'Sir/Madam'; }             
                //Set to addresses to customer AND standardsupport@binarytree.com
                String[] toAddresses = new String[] {sharedMailboxEmail};
                         //Add the email2case address so the message is added to the case emails
                 if (eM.CcAddress !=null){
                      String[] ccAddresses = eM.CcAddress.split(';'); //new String[] {eM.CcAddress};
                      system.debug('CC Addresses:  ' + ccAddresses);
                mail.setSenderDisplayName('<COMPANY NAME> Support');
                mail.setSubject('Case: ' + ca.CaseNumber +': '+ eM.Subject + ' '+ refI);
                mail.setPlainTextBody('ORG_ID: [4AA2839E69]' + eM.TextBody);
                mail.setHtmlBody('Dear ' + nameContact + ', <br><br>Thank you for contacting <COMPANY NAME> Support.  Case#: ' + ca.CaseNumber + ' has been created and a support representative will respond shortly.<br><br>All responses should be from this email thread. Always use the "Reply All" button<br><br><b> - <COMPANY NAME> Support</b><br><br>' + eM.HtmlBody+ 'Reference: ' + refI );
               // mail.setHtmlBody('Customer Email: ' + eM.FromAddress + '<br>' + 'Case#: ' + ca.CaseNumber + '<br>' + 'Reference: ' + refI + '<br><br>' + eM.HtmlBody ); 
                //Loop through the attachments and add them to the email
                List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                System.debug('Number of Attachments: ' + fileAttachments.size() );
                for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :eM.Id]){
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                    System.debug('attachment file name is '+ a.Name + ' for message: ' +eM.Id); //JSR Debugger
                //Add this email to the list of emails to send
                //*****EMAIL TO A SECONDARY TICKETING SYSTEM****** 
                //Create a new email message to send to backup ticketing system - No Attachments or Body from original email
                Messaging.SingleEmailMessage vMail = new Messaging.SingleEmailMessage();
                 if (nameContact == null){  nameContact = 'Unknown'; }  //Email Sender Name
                //Set to addresses to ticket system Email
                 String[] toVAddresses = new String[] {VEmailAddr}; 
                vMail.setReplyTo('noreply@<COMPANY NAME>.com'); 
                vMail.setSenderDisplayName('<COMPANY NAME> Support');
                vMail.setSubject('Case: ' + ca.CaseNumber +': '+ eM.Subject +' - '+ refI); 
                vMail.setPlainTextBody('Customer Email: [' + eM.FromAddress + ']<br>' +  +'<br>'+ 'Customer Name: ' + nameContact + '<br>' +'Case#: ' + ca.CaseNumber + '<br>' + 'Reference: ' + refI );
                vMail.setHtmlBody('Customer Email: [' + eM.FromAddress + ']<br>' +'Customer First Name: [' + nameContact + ']<br>' + 'Case#: ' + ca.CaseNumber + '<br>' + 'Reference: ' + refI); //Body of email to Secondary ticketing system
                //Add this email to the list of emails to send
                //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('SendEmailInfo: ' + error[0].getFields() + ' / ' + error[0].getTargetObjectId() + ' / ' + error[0].getMessage());