Skip to main content
13 votes

Files as email attachment from Apex

If you have the ContentDocument Id you can fetch the ContentVersion file as this: // If you only want the ContentVersion ID's Map<Id, ContentVersion> contentVersions = new Map<Id, ...
Simonp's user avatar
  • 1,902
12 votes
Accepted

How to send an Email Template via Apex?

In your code you need to bring in a slight change. Where you setting AccountId in setWhatId() method, change it to mail.setWhatId(ContactId); A brief explanation from the docs. setWhatId(whatId) ...
LetMeCodeYou's user avatar
9 votes
Accepted

Send an Email functionality in a custom button of a custom object

As far as the default functionality concerned. All you have to do is follow the steps described below and you'll have your custom button linked with the default Salesforce email author. Create a ...
LetMeCodeYou's user avatar
9 votes
Accepted

System.LimitException: Too many SOQL queries: 101

Hi You are getting the error because you are using Messaging.renderStoredEmailTemplate() in for loop as this is counted towards the SOQL governor limit as one query. To avoid the exception you have to ...
Rishi Golyan's user avatar
9 votes

System.LimitException: Too many SOQL queries: 101

Rishi Golyan correctly identified the issue, however, Messaging.renderStoredEmailTemplate() works on single ids rather than a list of ids. So, simply moving that method call to be outside of your ...
Derek F's user avatar
  • 64.1k
9 votes
Accepted

Messaging.SendEmailResult Type cannot be constructed?

Yes you can construct the Messaging.SendEmailResult using the JSON deserializer Messaging.SendEmailResult er =(Messaging.SendEmailResult) JSON.deserialize('{}', Messaging.SendEmailResult.class); ...
Pranay Jaiswal's user avatar
8 votes

How to get the Id of an EmailMessage after sending a SingleEmailMessage with Messaging.SendEmail()?

You can't get the Id on the same transaction because the email messages are sent after the transaction succeeds. See items 19 and 20 on the Triggers and Order of Execution: Commits all DML ...
Renato Oliveira's user avatar
7 votes
Accepted

Not Serializable: com/salesforce/api/fast/List$$lcom/salesforce/api/Messaging/SingleEmailMessage$$r

The only reason Salesforce is trying to serialize the List<Messaging.SingleEmailMessage> property is because you are adding it to the View State. You don't need to, however, as you do not ...
Adrian Larson's user avatar
  • 151k
7 votes

Test class Limits.getEmailInvocations() showing zero and assertion is failing

The reason is because the test.starttest and test.stoptest create a new apex context. Inside the context it will show 1. so try: Test.startTest(); update recordsToUpdate; System.assertEquals(1, ...
gNerb's user avatar
  • 9,588
7 votes
Accepted

In Messaging.SendEmailResult what does the errors attribute contain?

errrors is a list of SendEmailError. You can Iterate and collect errors: for (Messaging.SendEmailResult mr : mrs) { if (mr.isSuccess()) { //Do something for success } else { ...
manjit5190's user avatar
  • 8,073
7 votes
Accepted

Code optimisation on EmailMessage

Don't Skimp on Curly Brackets You may think that you're saving some space/typing with: if (em.Incoming) caseIds.add(em.parentId); But you're actually risking a very subtle bug. Commenting a ...
sfdcfox's user avatar
  • 502k
6 votes
Accepted

Sending VF email template using SingleEmailMessaging- Getting blank Email body

Following link has an answer to the question: Using APEX to assemble HTML Letterhead Emails Summer 16 introduced new method: Messaging.SingleEmailMessage email = Messaging....
SfdcBat's user avatar
  • 4,839
6 votes
Accepted

How does setTreatBodiesAsTemplate(treatAsTemplate) work?

It works exactly as the documentation lays out. Optional. If set to true, the subject, plain text, and HTML text bodies of the email are treated as template data. It basically allows you to ...
Adrian Larson's user avatar
  • 151k
6 votes

Messaging: setTargetObjectID with custom objects

To avoid the error, use setTreatTargetObjectAsRecipient to false: email.setTreatTargetObjectAsRecipient(false);
sfdcfox's user avatar
  • 502k
5 votes
Accepted

How to set time limit between queued jobs?

This is from Dan Appleman's Advanced Apex Programming Ver 3. The code is available for download from his web site at http://www.AdvancedApex.com This pattern uses a dispatcher class that implements ...
crmprogdev's user avatar
5 votes

Send mail to the System Administrator

You can do it with just one query: SELECT Email FROM User WHERE Profile.Name = 'System Administrator'
user's user avatar
  • 61
5 votes

How many single email messages with visualforce templates can be sent in a single transaction?

Messaging.SingleEmailMessage needs one contact per template in order to merge the data correctly (including Contact-specific information). The good news is that Messaging.sendEmail supports at least ...
sfdcfox's user avatar
  • 502k
5 votes

For the Platform Event "Automated Process" user, why do both Visualforce controller access and email sending fail?

What helped in our project regarding the class access, for example when process builder is calling InvocableMethod annotated apex method and requires access to the class, was to assign the Automated ...
Michal V's user avatar
  • 143
5 votes

Messaging.SendEmailResult Type cannot be constructed?

Since the properties of this class are not in the official salesforce documentation, hopefully this helps someone. If you need to create an instance of this that return false for isSuccess() ...
Nick Stuempges's user avatar
5 votes
Accepted

Apex SingleEmail using template and userId

This is expected behavior the User fields you are referencing are the sending user fields User fields are also known as Sending User fields for administrators and users with the “Manage Users” ...
thegogz's user avatar
  • 368
5 votes
Accepted

sending html email from APEX using templates

If you are using an email template, want to send an email using that template (and can afford for Salesforce to perform a query to load that template) and have mailmerge in the template, the easiest ...
Phil W's user avatar
  • 38.1k
4 votes
Accepted

How can I write a test class for SingleEmailMessage trigger and helper class?

You're on the right track thinking that this has to do with custom settings. Line 12 in your EmailUtility class is Id emailTemplateId = Id.valueOf(sti.email_template_id__c); In unit tests, Custom ...
Derek F's user avatar
  • 64.1k
4 votes
Accepted

Email not delivered, but registered as delivered by the system

Have you checked your DKIM keys? Yes I ha... oh, no. I have not. Turns out that my Sandbox instance didn't have the DKIM keys used to sign emails sent through the platform. Meaning that the message ...
Renato Oliveira's user avatar
4 votes
Accepted

Messaging.SingleEmailMessage method compiles anonymously but not in a class

The newest API Version is not 34.0, but rather 38.0, in which this method will compile. It looks like this method was added in API Version 35.0, as it compiles for any version 35.0 and above. Simple ...
Adrian Larson's user avatar
  • 151k
4 votes

Remove Email Footer while sending an email to contact

This question is very old, but like me, other people may have it even today. All my google searching hasn’t turned up an answer. So I will share what I have found. The text: Powered by Salesforce ...
Shawn Sabbini's user avatar
4 votes

How to set time limit between queued jobs?

It's easier with Batchable, which allows you to call: public static String scheduleBatch(Database.Batchable batchable, String jobName, Integer minutesFromNow) In theory, you can use schedules and ...
Adrian Larson's user avatar
  • 151k
4 votes

How to send an Email Template via Apex?

You need to remove below 2 lines to solve empty email. mail.setToAddresses(toAddress); mail.setSubject('test subject'); once you provide the code with some part of mail(like subject, body) then it ...
SSatoshi's user avatar
4 votes

Email Attachment (CSV) Issue

This appears to be classic Microsoft Excel behavior when confronted with UTF-8 text. Excel likes to interpret a valid UTF-8 bytestream as if it were encoded in Windows Codepage 1252 or other encodings....
David Reed's user avatar
  • 93.7k
4 votes
Accepted

For the Platform Event "Automated Process" user, why do both Visualforce controller access and email sending fail?

Salesforce support has confirmed that the "Automated Process" user's permissions are lacking key ones and that there is no way to add permissions. And that the ability to switch to a ...
Keith C's user avatar
  • 137k
4 votes
Accepted

how to remove unwanted things from mail sent from salesforce?

Florian's comment explains that the "Sandbox" prefix will disappear in Production. For the senderDisplayName and mail id of the user showing, what's the value of what you're passing into ...
Kris Goncalves's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible