How to relate Lead to EmailMessage and EmailMessageRelation?

How to relate Lead to EmailMessage and EmailMessageRelation?

Sample Code:
EmailMessage emailMessage = new EmailMessage(); 
emailMessage.status = ‘3’; // email was sent  
emailMessage.fromName = ‘Magulan Duraipandian’; // from name  
emailMessage.subject = ‘This is the Subject!’; // email subject  
emailMessage.htmlBody = ‘<html><body><b>Hello</b></body></html>’; // email body  
//Lead Id  
String[] toIds = new String[]{’00Q3i0000018EKHEA2′};   
emailMessage.toIds = toIds;  
// additional recipients who don’t have a corresponding contact, lead or user id in the Salesforce org (optional)  
//emailMessage.toAddress = ’[email protected], [email protected]’;  
insert emailMessage; // insert  
 
// Add Email Message Relation for id of the sender  
EmailMessageRelation emr = new EmailMessageRelation();  
emr.emailMessageId = emailMessage.Id;  
emr.relationId = ’00Q3i0000018EKHEA2′; // Lead Id  
emr.relationType = ‘FromAddress’;  
insert emr; 

2 thoughts on “How to relate Lead to EmailMessage and EmailMessageRelation?

Leave a Reply