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
suri03081988suri03081988 

Regarding Email-To-Case and auto-Response rules - Requirement is to override the ToAddress

we have Email-to-Case and auto response rule configured and we are writing a logic in before insert trigger to update a case field "SuppliedEmail" with required email
 
Requirement:
Customer ABC (abc@soft.com) is sending email to a Middle email (def@soft.com). the email is forwarding from def@soft.com to common support email (support_APM@soft.com).  outlook forwarding settings are forwarding the email from "support_APM@soft.com" to Salesforce email-to-case generate long email. Now the auto response rule is sending the acknowledge email to "def@soft.com". But expectation is to send acknowledge email notification to abc@soft.com
 
To achieve the requirements we are using before insert / before update /after Inser logic to override the def@soft.com to abc@soft.com

But no use of above trigger logic. Still the auto response notification is sending to def@soft.com instead of abc@soft.com

Could some one please help me on this..

Thanks,
Surendra
SubratSubrat (Salesforce Developers) 
Hello Surendra ,

Based on your description, it seems that you want to override the "To" address in the auto response email sent by Salesforce Email-to-Case. You've mentioned that you tried using a trigger to update the "SuppliedEmail" field with the required email, but it doesn't seem to be working as expected.


Here's a sample code snippet which you can try :
trigger EmailMessageTrigger on EmailMessage (before insert) {
    List<EmailMessage> emailMessagesToUpdate = new List<EmailMessage>();
    List<Case> casesToUpdate = new List<Case>();

    for (EmailMessage emailMessage : Trigger.new) {
        if (emailMessage.ParentId.getSObjectType() == Case.SObjectType) {
            Case caseObj = new Case(Id = emailMessage.ParentId);
            caseObj.SuppliedEmail = emailMessage.FromAddress;
            casesToUpdate.add(caseObj);

            emailMessage.ToAddress = 'abc@soft.com';
            emailMessagesToUpdate.add(emailMessage);
        }
    }

    if (!emailMessagesToUpdate.isEmpty()) {
        update emailMessagesToUpdate;
    }

    if (!casesToUpdate.isEmpty()) {
        update casesToUpdate;
    }
}


Make sure to adapt the code to fit your specific requirements and field names. After implementing this trigger, the "To" address in the auto response email should be overridden with "abc@soft.com" for the scenario you described.

If this helps , please mark this as Best Answer.
Thank you.
suri03081988suri03081988
Hi Subrat,

Thanks for the response. I will check it at my end

Thanks,
Surendra
suri03081988suri03081988
Hi Subrat,

Still email is sending to def@soft.com, but i can see the toaddress as abc@soft.com when i checked the emailMessage records under case record.

Please advise me, i think we almost there but result is not working.

Thanks,
Surendra