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;