In the Case object, we have OOB Contact lookup(ContactId). In my project case is created using 3 methods, WebtoCase, EmailToCase, and PhoneToCase. Some time the person who created the case might live the company or will be shifted to another department. At the time the Contact on the Case record will be updated. When a Call Center agent wants to send an email to that contact, in ToAddress of SendEmail the current Contact of the case record is getting populated for WebToCase and PhoneToCase but for EmailToCase the person who initially created the case that person's contact is getting populated not the current case contact. I contacted SF support and they suggested me to use:


Here is the code I am using for SendEmail quick action.

global class SendEmailQuickAction implements QuickAction.QuickActionDefaultsHandler {
    global SendEmailQuickAction() {
    global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
        Id ContId;
        QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = (QuickAction.SendEmailQuickActionDefaults)defaults.get(0);
        if(sendEmailDefaults.getActionName() == 'Case.SendEmail1' && sendEmailDefaults.getActionType() == 'SendEmail')
            ContId = sendEmailDefaults.getContextId();
            EmailMessage varemailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();   
            Case varCase =  [SELECT Contact.Email,Contact.Name,Contact.Id,ContactId from Case Where Id = :ContId Limit 1];
            varemailMessage.ToAddress = '';
            varemailMessage.ToAddress = varCase.ContactId;

Now issue is

  1. I am not able to populate Contact. When I use ContactID I am seeing the ID, not the contact Name.
  2. If I update the case contact multiple times I see all old send email contact in ToAddress.

enter image description here

I need some help with ToAddress. Can someone help me pointing what mistake I am doing in the code. My requirement is ToAddress should have only Case Contact. This issue is happening for Email to Case. For other type of case i dont have this issue.

  • To be honest, I'm a little lost/confused, as I am unfamiliar with some of this. But have you tried using the OOB field ContactEmail that is on the Case object?
    – Moonpie
    Aug 21, 2021 at 20:29
  • @Moonpie Sorry I was not a little clear with my question. I have updated my question.
    – Tamil
    Aug 22, 2021 at 16:56
  • 1
    ContactEmail uses the ContactId lookup, so when you change the Case Contact and save, ContactEmail is automatically updated. I was asking whether you had tried to use ContactEmail (NOT Contact.Email <-- notice the '." dot) in your above code? Maybe including it in your SELECT statement and making it your varemailMessage.ToAddress?
    – Moonpie
    Aug 22, 2021 at 20:52
  • @Moonpie I am still seeing all previous contact in ToAddress. In ToAddress I just need to see only the Current Case Contact.
    – Tamil
    Aug 23, 2021 at 13:15
  • Please clarify: Are you using the code you have shown for all three Case types, and the shown code works for the other two just not for Email-to-Case?
    – Moonpie
    Aug 23, 2021 at 16:12

1 Answer 1


In addition to setting

    varemailMessage.ToAddress = '';

you should also set,

    varemailMessage.ToIds = ToIds;

to ids to be fetched like so

    String [] ToIds = new String []{varCase.ContactId};

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.