You need to sign in to do that
Don't have an account?
Marx Prathap Singh Shanmugam
Hi all, using apex i need to insert custom data to an EmailMessage object.the record need to assigned to a contact's Activity. how to achieve this??
Here is my code
Contact cts= new Contact();
cts.Id=[SELECT Id FROM Contact Where Email='contact@email.com'].Id;
System.debug('Contact id'+cts.Id);
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.FromAddress='fromaddress@email.com';
emailMsg.ToAddress='toaddress@email.com';
emailMsg.Subject='Email using Apex';
emailMsg.RelatedToId=cts.Id;
emailMsg.HtmlBody='Temp Email using apex code';
emailMsg.ParentId = null; //Attach with the case
emailMsg.MessageDate = system.now();
emailMsg.Status = '0';
insert emailMsg;
Contact cts= new Contact();
cts.Id=[SELECT Id FROM Contact Where Email='contact@email.com'].Id;
System.debug('Contact id'+cts.Id);
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.FromAddress='fromaddress@email.com';
emailMsg.ToAddress='toaddress@email.com';
emailMsg.Subject='Email using Apex';
emailMsg.RelatedToId=cts.Id;
emailMsg.HtmlBody='Temp Email using apex code';
emailMsg.ParentId = null; //Attach with the case
emailMsg.MessageDate = system.now();
emailMsg.Status = '0';
insert emailMsg;
public class Insert_Email {
public insert_email() {
Id id2=[SELECT Id FROM Contact Where Email='tvnaresh@gmail.com'].id;
System.debug('Contact id'+id2);
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.FromAddress='abc@yahoo.com';
emailMsg.ToAddress='abc@gmail.com';
emailMsg.Subject='Email using Apex';
emailMsg.RelatedToId='0060I00000UEA4KQAX';// related to opportunity
emailMsg.HtmlBody='Temp Email using apex code';
emailMsg.ParentId = null; //Attach with the case
emailMsg.MessageDate = system.now();
emailMsg.Status = '0';
insert emailMsg;
}}
It is showing the below error.
I have just modified the emailm from address,to address and the relatedtoID
and my code is,
public class Insert_Email {
public insert_email() {
Id id2=[SELECT Id FROM Contact Where Email='marxprathap_ava@hotmail.com'].id;
System.debug('Contact id'+id2);
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.FromAddress='marxprathap_ava@hotmail.com';
emailMsg.ToAddress='Marx@AvaPrathap.onmicrosoft.com';
emailMsg.Subject='Email using Apex';
emailMsg.RelatedToId='0037F00000PmptPQAR';// related to opportunity
emailMsg.HtmlBody='Temp Email using apex code';
emailMsg.ParentId = null; //Attach with the case
emailMsg.MessageDate = system.now();
emailMsg.Status = '0';
insert emailMsg;
}
}