function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jai SureshJai Suresh 

EmailMessages created with Apex not listed under Case

I am trying to create a case and send an email with attachment related to the case.
Both are working fine, but the email is not listed under the respective case, but the attachment is properly listed.

Here is the code:
for (Case cRow: lstcase) 
	{
		//....
		String[] arrSendEmailAdd = new String[]{};
		arrSendEmailAdd = toAdd.split(',');

		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
		email.setToAddresses(arrSendEmailAdd);

		String mailSubject = 'Your Sterling Membership E Kit-ID - '+cuRow.Name;
		String mailMessage = '';
		email.setSubject(mailSubject);
		email.setHtmlBody(mailMessage);
		email.setSaveAsActivity(false);
		email.setWhatId(cRow.Id);
		//...
		//Attachment
		//....
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
	}

 
Best Answer chosen by Jai Suresh
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

This is an expected behavior. When you send an e-mail through Salesforce standard e-mail page it creates a Task with the e-mail informations and links it with the related object (WhatId). However when you sends using apex it does not happens. One workaround would be reproducing the normal behavior, i.e. (you can change it as you wish):
 
String desc = String.format('Aditional to: {0}\nCC: {1}\nBCC: {2}\nAttachment: {3}\n\nSubject: Email: {4}\nBody: {5}',
				new String[]{
					arrSendEmailAdd == null ? '' : arrSendEmailAdd,
					cc == null ? '' : cc,
					bcc == null ? '' : bcc,
					attach == null ? '' : attach.Name,
					mailSubject == null ? '': mailSubject,
					mailMessage == null ? '': mailMessage
				}
			);

	Task tsk = new Task();

	tsk.OwnerId = UserInfo.getUserId();
	tsk.WhatId = cRow.Id;
	tsk.Subject = 'Email: ' + mailSubject;
	tsk.Description = desc;
	tsk.Priority = 'Normal';
	tsk.Status = 'Completed';
	tsk.ActivityDate = System.today();
	tsk.Type = 'Email';
	Database.insert(tsk);

The method setWhatId of  SingleEmailMessage class helps to further ensure that merge fields in the template contain the correct data, it does not links it to the object referenced.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

This is an expected behavior. When you send an e-mail through Salesforce standard e-mail page it creates a Task with the e-mail informations and links it with the related object (WhatId). However when you sends using apex it does not happens. One workaround would be reproducing the normal behavior, i.e. (you can change it as you wish):
 
String desc = String.format('Aditional to: {0}\nCC: {1}\nBCC: {2}\nAttachment: {3}\n\nSubject: Email: {4}\nBody: {5}',
				new String[]{
					arrSendEmailAdd == null ? '' : arrSendEmailAdd,
					cc == null ? '' : cc,
					bcc == null ? '' : bcc,
					attach == null ? '' : attach.Name,
					mailSubject == null ? '': mailSubject,
					mailMessage == null ? '': mailMessage
				}
			);

	Task tsk = new Task();

	tsk.OwnerId = UserInfo.getUserId();
	tsk.WhatId = cRow.Id;
	tsk.Subject = 'Email: ' + mailSubject;
	tsk.Description = desc;
	tsk.Priority = 'Normal';
	tsk.Status = 'Completed';
	tsk.ActivityDate = System.today();
	tsk.Type = 'Email';
	Database.insert(tsk);

The method setWhatId of  SingleEmailMessage class helps to further ensure that merge fields in the template contain the correct data, it does not links it to the object referenced.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Have you solved your problem?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.