I am working on service cloud. In our system the cases are created from email-to-case. we have other system in which we have customer data. The emails are being forwarded from other system to salesforce to create case. In the headers we are putting the account and contact information which helps us to link the case to contact and account.

Actual problem: In the auto response we want to use merge fields from account and contact. For this, I have checked case before and after trigger but this dosent work as firstly case is created then the Email message is created which is linked to case. So, I am not able to find the email message in case trigger. so I have to put the linking logic to emailmessage Trigger. but this happends after the case is inserted. so the auto response is having blank account and contact values.

Please share any work around for this.

Thank you soo much in advance!!

2 Answers 2


Firstly, If you are using email-to-case and if the incoming email is from an contact's email id (from email from an contact's email field) salesforce will automatically associate the case with Contact and Account (related to contact's email - Out of the box functionality). in this case you will be able to find contact Id in before insert. this should be your recommended solution

enter image description here

but, if your incoming email is not associated with any of your contacts, then you may need to discard the auto-response email and send the email from WF/Time-based WF once your fields on the case is populated. let me know what you think..

  • In our case the email are not from contacts. Commented Dec 5, 2019 at 12:33
  • Did you try discarding the auto-response email and send the email from WF/Time-based WF once your fields on the case is populated.
    – Veera G
    Commented Dec 5, 2019 at 12:52
  • I would like to stick to auto response. on side note: Updated values of account and contact will be used as merge field when sending time dependent wf email alert. of the older values when the rule was triggered. Commented Dec 5, 2019 at 12:58

If you are determined to keep the auto-response rules; you can do the following:

  • Add a condition to every rule that ensures Case.ContactId is present

When the E2C comes in initially, the auto-response rules execute but if there's no ContactId, they do nothing

  • In your trigger on EmailMessage, after populating Case.ContactId but before saving the record, use the Database.DmlOptions and tell Salesforce to run the auto-response rules

Here's the basic outline of how to solve this (untested)

trigger EnhanceCase on EmailMessage (after insert) { 
   Case[] enhancedCases = new List<Case> ();  
   for (EmailMessage em: Trigger.new) {
       if (em.ParentId == null || em.ParentId.getSObjectType() != Case.SObjectType) {continue;}
       Case cs = new Case (
         Id = em.ParentId,
         ContactId = ..your logic here ...
       DmlOptions autoResponseOptions = new Database.DMLOptions(); 
       autoResponseOptions.EmailHeader.triggerAutoResponseEmail = true;
       cs.setOptions( autoResponseOptions);
   update enhancedCases; // DMLOptions header fires auto-response rules

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .