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 = 'emailnotinsalesforce@toexample.com, anotherone@toexample.com';
- 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;
I have written EmailMessage and EmailMessageRelation object logic in Batch class as per my requirement, I also wrote Test class for the same but I am getting error on last line in execute method(insert emr) while running the test. Test class is covering the entire class but giving error on last line, can you please share the test class of above code so that I can refer it from here. Thanks in advance.
ReplyDeleteCheck why the insert is failing.
DeleteThere shouldn't be any issue with the test class.
https://www.infallibletechie.com/2020/05/test-class-for-emailmessage-and.html