2

I've made several attempts to save an email message to be saved into the Activity history of opportunity.

I have a vf page which has a form to send an email. I can send the email but not able to insert it into the salesforce so that users can refer to it later on, what they sent to their opportunities.

First I was trying to provide the OpportunityId as the ParentId of EmailMessage instance. And received the following error.

Parent Case: Case ID: id value of incorrect type: 18-digit Id. Exception thrown trying to send Email Message. Details = System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case ID: id value of incorrect type: 18-digit Id: [ParentId]

Now even I tried with by creating a task and link it with the opportunity. And make the task as the parent to the EmailMessage instance. But no success.

I not providing the full code just to keep it short and also because of the reason that there is no issue with sending the email. But with linking it with the opportunity. As you can see below I tried to trim the 18-digit Id to 15-digit Id but no success. I thought that this could be an issue due to the error message. id value of incorrect type.

    Task t = new Task();
    t.WhatId = thisPosition.Id;
    t.OwnerId = UserInfo.getUserId();
    t.Subject = 'Test';
    t.Priority = 'High';
    t.Status = 'Test Status';
    t.Description = 'Test Description';
    insert t; 

    // Id taskId = String.valueOf(t.Id).substring(0,15);

    // populate fields with values from SingleEmailMessage.
    emailMsg.BccAddress  = UserInfo.getUserEmail();
    emailMsg.Subject     = dummyEmailMsg.getSubject();
    emailMsg.TextBody    = dummyEmailMsg.getPlainTextBody();
    emailMsg.ToAddress   = dummyEmailMsg.getToAddresses().get(0);
    emailMsg.FromAddress = SUPPORT_EMAIL_ADDRESS; 
    emailMsg.CcAddress   = '';
    emailMsg.ParentId    = t.Id;

2 Answers 2

1

If the issue is only about linking it to an object then this is not a big issue. All you need to do is to create a case instead of Task.

Because emails are no longer stored as tasks, but as full-featured records, complete with details, related lists, and collaboration tools. On email records, users can view their messages exactly as their customers do, complete with formatting.

Link your emailMsg with the case SourceId and provide your opportunity Id to emailMsg.RelatedToId. You can omit the ParentId as it has nothing to do with showing under activity history. Try this.

  Case c = new Case();
  c.SourceId = emailMsg.Id;
  insert c;

  // populate fields with values from SingleEmailMessage.
  emailMsg.BccAddress  = UserInfo.getUserEmail();
  emailMsg.Subject     = dummyEmailMsg.getSubject();
  emailMsg.TextBody    = dummyEmailMsg.getPlainTextBody();
  emailMsg.ToAddress   = dummyEmailMsg.getToAddresses().get(0);
  emailMsg.FromAddress = SUPPORT_EMAIL_ADDRESS; 
  emailMsg.CcAddress   = '';
  emailMsg.RelatedToId = 'yourOpportunityId';

Also you can get hint from the error message that why it is saying about the case Id, when you're not using it at all in your code apparently.

0

If you want to save the email message to the activity history of your opportuniy, you don't need to insert a task manually.

All you have to do is specify a whatId on your SingleEmailMessage class by using the setWhatId(whatId) method. Like this:

emailMsg.setWhatId(opportunity.Id);
1
  • Yeah error is gone but can't see the email message in the activity history related list of opportunity.
    – Superman
    Commented Jun 28, 2017 at 12:27

You must log in to answer this question.

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