How to send out donation receipts to single or multiple email contacts?

For a non-profit organization, donations are an integral component. These donations are tax-deductible for the donors and hence, there is always a need for formal documentation that serves as an evidence of the donations made by them. Donation receipts hence have to be sent to the existing and prospective donors for record-keeping both by the individual donor as well as by the non-profit organization. With Salesforce, sending out yearly donation receipts becomes a simple task.

#1. Sending donation receipts to multiple email contacts:

In order to send yearly donation receipts to multiple email contacts, DML Governor limits and Email Governor limits will have to be considered. The below query will help to get the donation details for a specified year. In this single query, the donation object is queried rather than the contact object.

SELECT Id, Contact_Name__c, Contact_Name__r.Email, Contact_Name__r.Address,

Donation_Amount__c, Donation_Date__c

FROM Donations__c

WHERE CALENDAR_YEAR(Donation_Date__c) = year and Contact_Name__r.Email != null ORDER

BY Contact_Name__c, Donation_Date__c

The next step is to generate the donation lists for each donation and pass it on to a handler

method for further processing

for(Integer i = 0; i < totalDonations.size(); ++i){

List tempList = new List();

tempList.add(totalDonations.get(i));

i += 1;

while(i < totalDonations.size() && totalDonations.get(i).Contact_Name__r.Id ==

totalDonations.get(i – 1).Contact_Name__r.Id){

tempList.add(totalDonations.get(i));

i += 1;

}

if(!tempList.isEmpty())

receiptEmailsForAllContacts.add(getEmail(tempList));

}

The donations of each user is added to a ‘tempList’ and the list is passed to ‘getEmail’ method.

  • The ‘getEmail’ method takes the donation list as arguments and returns an email object with all the needed attachments; this will be added to the master list, which will eventually send out emails in a single operation.
  • The getEmail method uses a HTML Email Template in salesforce as an attachment which will be filled out with the user details and donation details before rendering It as a PDF and attaching it to the email.

private Messaging.SingleEmailMessage getEmail(List<Donations__c> donationList){

String attachmentTemplateHtmlValue;

String donationsTableHtml = ”;

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

attachmentTemplateHtmlValue = attachmentTemplate.HtmlValue;

year = donationList.get(0).Donation_Date__c.year();

subject = year + ‘ Tax Deductible Donation Receipt Summary’;

contactId = donationList.get(0).Contact_Name__c;

donations = donationList;

donor = new Contact();

donor.FirstName = donationList.get(0).Contact_Name__r.FirstName;

donor.LastName = donationList.get(0).Contact_Name__r.LastName;

donor.Email = donationList.get(0).Contact_Name__r.Email;

donor.MailingStreet = donationList.get(0).Contact_Name__r.MailingStreet;

donor.MailingCity = donationList.get(0).Contact_Name__r.MailingCity;

donor.MailingState = donationList.get(0).Contact_Name__r.MailingState;

donor.MailingPostalCode = donationList.get(0).Contact_Name__r.MailingPostalCode;

sumOfDonations = 0;

donationsTableHtml = ‘<table><tr><td>Donation Date</td><td>Donation

Amount</td></tr>’;

for(Donations__c dn : donations){

donationsTableHtml += ‘<tr><td>’ + dn.Donation_Date__c.format() + ‘</td><td>’ +

dn.Donation_Amount__c + ‘</td></tr>’;

sumOfDonations += dn.Donation_Amount__c;

}

donationsTableHtml += ‘</table>’;

Decimal decSumOfDonations = Decimal.valueOf(sumOfDonations);

totalSumOfDonations = String.valueOf(decSumOfDonations.setScale(2));

if(String.valueOf(year) != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<year>’,

String.valueOf(year));

if(donor.FirstName != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

first name>’, donor.FirstName);

if(donor.LastName != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

last name>’, donor.LastName);

if(donor.MailingStreet != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

mailing street>’, donor.MailingStreet);

if(donor.MailingCity != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

mailing city>’, donor.MailingCity);

if(donor.MailingState != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

mailing state>’, donor.MailingState);

if(donor.MailingPostalCode != null)

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donor

mailing zip>’, donor.MailingPostalCode);

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donations

table>’, donationsTableHtml);

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<donations

total>’, totalSumOfDonations);

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<ein>’,

configMap.get(‘Tax ID Number’));

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<director

name>’, configMap.get(‘Director Name’));

attachmentTemplateHtmlValue = attachmentTemplateHtmlValue.replace(‘<director

email>’, configMap.get(‘Director Email’));

// Take the PDF content

Blob b = Blob.toPDF(attachmentTemplateHtmlValue);

// Create the email attachment

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

efa.setFileName(year + ‘ Tax Deductible Donation Receipt Summary.pdf’);

efa.setBody(b);

String[] myEmail = new String[1];

myEmail[0] = donor.Email;

// Sets the paramaters of the email

email.setSubject( subject );

email.setToAddresses( myEmail );

email.setPlainTextBody( body );

email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

return email;

}

after all the email objects are built and added to the master email list, they will be sent in a

single operation.

Messaging.sendEmail(masterEmailList);

#2. Sending donation receipt to a single email contact:

We generate a general Visualforce page with a controller. The constructor in the controller

does the query to the donation object for that specific user, and the Visualforce page would

be attached in the PDF format to the email and sent.

This is the constructor of the controller

public DonationsOfUser() {

configList = new List<CFI_Config__c>();

configMap = new Map<String,String>();

configList = [SELECT Id, Name, value__c from CFI_Config__c];

for(CFI_Config__c con : configList){

configMap.put(con.Name, con.value__c);

}

if(ApexPages.currentPage() != null &&

!ApexPages.currentPage().getURL().containsIgnoreCase(‘/apex/sendReceiptsToAllContacts’)

){

year = System.Today().year() – 1;

contactId = ApexPages.currentPage().getParameters().get(‘id’);

donations = [SELECT Donation_Amount__c, Donation_Date__c

FROM Donations__c

WHERE Contact_Name__c = :contactId AND

CALENDAR_YEAR(Donation_Date__c) = :year];

subject = year + ‘ Tax Deductible Donation Receipt Summary’;

body = subject;

donor = [SELECT Name, Email, MailingStreet, MailingCity, MailingState,

MailingPostalCode

FROM Contact

WHERE id =:contactId];

sumOfDonations = 0;

for(Donations__c dn : donations){

sumOfDonations += dn.Donation_Amount__c;

}

Decimal decSumOfDonations = Decimal.valueOf(sumOfDonations);

totalSumOfDonations = String.valueOf(decSumOfDonations.setScale(2));

}

The email will be sent in the send method, which is invoked by an action from the vf page

public PageReference send() {

List<Messaging.SingleEmailMessage> receiptEmails =

new List<Messaging.SingleEmailMessage>();

// Define the email

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

// Reference the attachment page and pass in the account ID

PageReference pdf = Page.samplepdf;

pdf.getParameters().put(‘id’,contactid);

pdf.setRedirect(true);

// Take the PDF content

Blob b;

if (Test.IsRunningTest())

b = Blob.valueOf(‘UNIT.pdf’);

else

b = pdf.getContentAsPDF();

// Create the email attachment

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

efa.setFileName(year + ‘ Tax Deductible Donation Receipt Summary.pdf’);

efa.setBody(b);

// body = ‘Please find the attachment of the Requested Tax Deductible Document.’;

String[] myEmail = new String[1];

myEmail[0] = donor.Email;

// Sets the paramaters of the email

email.setSubject( subject );

email.setToAddresses( myEmail );

email.setPlainTextBody( body );

email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

receiptEmails.add(email);

// Sends the email

sendReceiptEmails(receiptEmails);

return null;

}