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
krishna chaitanya 35krishna chaitanya 35 

Auto forward emails that are received to case to a outlook email id

Hi Team,
I have a scenario like i need to auto forward the emails related to a case to a outllok email .

For Ex: For Case #00001 i have a received email i need to send those emails to test@test.salesforce.com.please kindly help me how to proceed with this implementation.

Thanks in Advance,
Krishna.
pconpcon
You could always create a trigger on the EmailMessage [1] object, check to see if ParentId is for a Case and then create a new EmailMessage to your outlook email address and send it that way.

[1] https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm
krishna chaitanya 35krishna chaitanya 35
Hi Pcon,

Can you please provide any sample code regarding the scenario.

Thanks in Advance,
Krishna.
pconpcon
trigger EmailTrigger on EmailMessage (after insert) {
    List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();

    Messaging.reserveSingleEmailCapacity(Trigger.new.size());
    
    for (EmailMessage em : Trigger.new) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> toAddresses = new List<String> {
            'user@acme.com' // Change to outlook address
        };
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('support@acme.com');
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('New Case Created : ' + em.ParentId);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody('Your Case: ' + em.ParentId +' has been created.');
        mail.setHtmlBody('Your case:<b> ' + em.ParentId +' </b>b>has been created.<p>');

        emailsToSend.add(mail);
    }   
        
    if (!emailsToSend.isEmpty) {
        Messaging.sendEmail(emailsToSent);
    }
}