Circumventing Salesforce Email Limits

In News by admin

Email Limit in Salesforce

When you need to send an email with a pdf attachment from your org, the easiest way to do so is using the Messaging or SingleEmailMessage classes. The problem with these methods is they count against your org’s email limits (get more information on email limits here). If you’re going to be sending a lot of emails with pdf attachments, it won’t take long before you use your allotment. There is, however, an easy way to circumvent these restrictions: Email Alerts.

To use an email alert, the recipient will need to be a Contact, Lead or User in your org, but email Alerts don’t count against your daily 1,000 maximum apex email cap. They are capped instead by a limit of 1000 per license, up to a maximum of 2,000,000. Still, Salesforce does not make it easy to attach a pdf to an email alert, so some extra coding is going to be needed.

The Attachment

The first thing you’ll need to do is create a Visualforce component that you will render as a pdf to create the attachment. If you are already using a Visualforce page to create the pdf, it won’t take much work to turn it into a component. You can even refactor your current processes to use the new component so you won’t need to keep two versions of the same markup up to date by replacing the markup in the VF page with a call to the new component. If you’re creating your pdf in some other fashion, you may need to reproduce it using VF.

You’ll need to declare an attribute. This is the how your email template will pass the related object into the component and, from the component, into the controller. Notice, in the above example, we’ve declared the component’s access as global. If you don’t do that, your email template won’t be able to access your component. We’ve also specified the controller for the component. If you started with a VF page, you probably already have one, if not, you’ll need to write one. Don’t worry, it’s easy.

The Controller

The controller is how your component will interact with your data. Your ‘assignTo’ variable will be the input parameter for the controller, so make sure they have the same name. You’ll also need to get your data from the controller in the markup, so create properties for any variables in the controller you’ll need to access.

The Email Template

The next step is to create the email template for the email that will be sent in the alert. Navigate to Administer > Communication Templates > Email Templates and select ‘New Template’.  In step one, select the option to create a Visualforce template. Make sure to check the ‘Available for use’ checkbox so it will be accessible to your org and give it a name. Create a subject line and recipient type. The Related to Type is the object you’ll be passing into the component and controller.

In your opening <messaging:attachment/> tag, declare a renderAs=”PDF” attribute and give the attachment a filename. In between the tags, call your component, passing the related object’s Id. Once you’ve got the template tied to your component, you’ll just need to create a workflow rule that fires an email alert when your data meets some criteria and you’ve successfully bypassed the messaging limits of your org.