0

I send an email from apex from opportunity object to account.

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();             
 mail.setSenderDisplayName('Test');
 mail.setHtmlBody(htmlbody);
 mail.setPlainTextBody(body);
 mail.setSubject(subject);
 mail.setTargetObjectId(c.Id);               
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

c.id is the contact id. It will automatically create a record emailmessage. But if I wanted to hook the email to the opportunity activity from which I send the email I get two email messages record (one for opportunity Not tracked and tow on account (one tracked and one else not tracked). Why?

EmailMessage emailMessage = new EmailMessage();
emailMessage.status = '3'; 
emailMessage.relatedToId = o.Id;      
emailMessage.fromName = 'PrestiYou'; 
emailMessage.subject = subject; 
emailMessage.htmlBody = htmlBody;    
emailMessage.toAddress = o.Email__c ; 

String[] toIds = new String[]{c.Id}; 
emailMessage.toIds = toIds;
insert emailMessage;        

The logic of sending mail is invoked by an opportunity trigger. I need to "track" the e-mail sent in order to monitor it. I need to find an emailMessage record on opportunity activity and on account activity tracked both. How ?

  • 1
    Can you add some of the trigger code? This could be a trigger-recursion issue – Sebastian Kessel Mar 29 at 17:16
  • even if I execute the code from execute anonymous I have the same problem – maidenfp Apr 1 at 8:41
  • If it’s a trigger problem, you would. But we have not seen your code. – Sebastian Kessel Apr 1 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.