2

I am creating a custom email client and have all the functionality needed using SingleEmailMessage.

However, I am unable to set a parentId on the SingleEmailMessage. I've tried looking on previous questions and saw people using mail.setSaveAsActivity(true); but its not saving it under a case's related lists.

On EmailMessage there is a parentId field that can be set to the case. But I do not see any examples of anyone sending EmailMessages.

Are EmailMessages basically receipts from a SingleEmailMessage? How can I get the parentId to attach to a case?

public PageReference sendEmail(){
    List<String> recipients = new List<String>();
    List<String> ccs = new List<String>();
    List<String> bcs = new List<String>();
    recipients.add(email);


    for(String additional : additionalTo.split(';')){
        if(additional.trim().length() > 0){
            recipients.add(additional.trim());
        }
    }
    for(String carbonCopy : cc.split(';')){
        if(carbonCopy.trim().length() > 0){
            ccs.add(carbonCopy.trim());
        }
    }
    for(String blindCopy : bc.split(';')){
        if(blindCopy.trim().length() > 0){
            bcs.add(blindCopy.trim());
        }
    }

    Integer numOfEmails = recipients.size() + ccs.size() + bcs.size();
    Messaging.reserveSingleEmailCapacity(numOfEmails);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(recipients);
    mail.setCcAddresses(ccs);
    mail.setBccAddresses(bcs);
    mail.setReplyTo(fromAddressesMap.get(fromAddress));
    System.debug('To Address: ' + email);
    mail.setSenderDisplayName('The Vitality Group Customer Support');
    mail.setSubject(subject);
    mail.setBccSender(false);
    mail.setUseSignature(false);
    mail.setPlainTextBody(body);
    mail.setSaveAsActivity(true);
    if(emailAttachment != null){
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(fileName);
        efa.setBody(emailAttachment);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    }
    if(memberCase != null){
        mail.setSubject(subject + ' ref: ' + memberCase.Id);
        mail.setWhatId(memberCase.Id);
    }
    else{
        mail.setTargetObjectId(con.Id);
    }
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    System.debug(r);
    return new PageReference('/' + con.Id);  
}
6

EmailMessage is a type object that has to do recording emails that are sent/received on cases. You don't use this object to send emails on cases, but simply use this object to log the data. Use Messaging.SingleEmailMessage. Here's a minimal version that you need to send an email, and have it appear on the case:

Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTargetObjectId(someContactId);
m.setWhatId(someCaseId);
m.setSaveAsActivity(true);
m.setSubject(subject);
m.setPlainTextBody(plainTextBody); // or HTML content using setHtmlBody.
Messaging.sendEmail(new Messaging.Email[] { m });
1
  • This is exactly what I ended up doing. Sending the message via SingleEmailMessage and using the EmailMessage as a receipt of the transaction – TemporaryFix Mar 8 '16 at 18:22
0

I think you want to set both the targetObjectIDs and the whatIDs rather than setting one or the other. If you use a template for your mass email your fields will be populated using these Ids like this:

// Send mass email to contacts of account associated with each updated case
for (Case uc : Trigger.new) {
  Contact[] contacts = [select HasOptedOutofEmail, Id from Contact where AccountId = :uc.AccountId];
  Id[] targetObjectIds = new Id[] {};
  Id[] whatIds = new Id[] {};

  for (Contact c : contacts) {
      targetObjectIds.add(c.Id);
      whatIds.add(uc.Id);
  }

  message.setTargetObjectIds(targetObjectIds);
  message.setWhatIds(whatIds);

You can track HTML emails, see more on that in this thread: https://success.salesforce.com/answers?id=90630000000glyaAAA

I hope that helps on your quest! Email is a nuanced thing in SF :)

4
  • I've tried this and targetObjectIds cannot be cases. We want the emails to be attached to a case if there is a case. Or else the contact if there is not a case – TemporaryFix Mar 4 '16 at 20:07
  • Hi Joey, the whatID is the object associated to email not the targetobjectID... so you will check for the existence of case and then assign the correct id using whatID. TargetObjectID is the contact record. I think you will always need a targetObjectID if using a template. – binaryLady Mar 4 '16 at 20:31
  • Will this attach the email to the email related list on the case object? – TemporaryFix Mar 4 '16 at 20:32
  • I believe it will be stored as an activity. If you use HTML email you can turn on tracking and view the email from the list. There is a workaround using emailmessage (this is the soap api format of the messaging method) to recreate the message and attach it to the case. It depends on what you need. If you just need tracking then it might be fine to have the activity or add a field to case and update it when email is sent using process builder or a trigger. --good luck! – binaryLady Mar 4 '16 at 21:07

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.