You are on page 1of 7

Email Programming:

==================
Drawbacks: ( Workflow Rules & Process Builder)
----------
1. Required an Email Template.
2. We can't add the Dynamic Attachments.
3. We can add max. of 5 CC Email Addresses.
4. These are purely supporting "Outbound Emails".(We can Send Emails)

--> Used to Send the Email Notifications to the External Email Id's.
--> By using Email Programming, we can send the Email Notifications with the
required
subject, content, attachments, To addresses, CC Addresses, BCC
Addresses,...etc.
--> It Supports both "Outbound Emails and Inbound Emails". (i.e. Send, Receive)

--> All the Email Programming related classes has been provided in the form of
"Messaging"
namespace.

Messaging NameSpace provides a collection readymade classes, to send /


receive the email notifications.

Outbound Email Features: (Send the Emails)


------------------------
Provides 2 readymade Classes as below.

1. Messaging.SingleEmailMessage Class
2. Messaging.MassEmailMessage Class

Inbound Email Features: (Receive the Emails)


-----------------------
Provides an Interface as below.

1. Messaging.InboundEmailHandler Interface
|
--> HandleInboundEmail()
Method

Messaging.SingleEmailMessage Class:
===================================
Syntax:
Messaging.SingleEmailMessage <objectName> = new
Messaging.SingleEmailMessage();

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

Methods:
--------
1. SetToAddresses(<List / Array>):
Ex:
string[] toAddress = new string[]
{'feroz.crm@gmail.com','ram@gmail.com'};
email.SetToAddresses(toAddress);

2. SetCCAddresses(<List / Array Name>):


Ex:
string[] ccAddress = new string[]
{'feroz.crm@gmail.com','ram@gmail.com'};
email.SetCCAddresses(ccAddress);

3. SetBCCAddresses(<List / Array Name>):


Ex:
string[] bccAddress = new string[]
{'feroz.crm@gmail.com','ram@gmail.com'};
email.SetBCCAddresses(bccAddress);

Note:
Total Number of email id's including "To Address, CC Address, BCC
Address" is max. of 150.

4. SetReplyTo(<String replyToEmailID>):

Ex:
email.SetReplyTo('customersupport@dell.com');

5. SetSenderDisplayName(String senderDisplayName):

Ex:
email.SetSenderDisplayName('ICICI Bank Credit Card Department');

6. SetSubject(String emailSubject):

Ex:
email.SetSubject('Email Subject');

7. SetPlainTextBody(string emailContent):

Ex:
email.SetPlainTextBody('Plain Text form of Email Content');

8. SetHTMLBody(string htmlEmailContent):

Ex:
email.SetHTMLBody('HTML Email Content');

9. SetFileAttachments(List<EmailFileAttachment> lstAttachments):
Ex:
email.SetFileAttachments(<List Collection>):

Send the Email:


---------------
Syntax:
Messaging.SendEmailResult[] results =
Messaging.SendEmail(List<Messaging.SingleEmailMessage>
lstEmails);

(OR)

Messaging.SendEmailResult[] results = Messaging.SendEmail(new


Messaging.SingleEmailMessage[]{<emailObject>});

UseCase:
========
Write an apex program, to Create a Two Lead Records in the object. And Send
the Email Notification to the Lead Persons with the required subject, Content and
Attachments.
Class Code:
-----------
public class LeadHandler
{
Public static void CreateLeadRecords()
{
List<Lead> lstLeads = new List<Lead>();

// First Lead Record..


Lead ld = new Lead();

ld.FirstName = 'Archana';
ld.LastName = 'Prakash';
ld.Title = 'Project Manager';
ld.Company = 'Cognizant Technology.';
ld.Status = 'Open - Not Contacted';
ld.Rating = 'Hot';
ld.Industry = 'Technology';
ld.AnnualRevenue = 3700000;
ld.Phone = '9900887766';
ld.Fax = '8899887766';
ld.Email = 'darchana214@gmail.com';
ld.LeadSource = 'Web';
ld.City = 'Hyderabad';
ld.State = 'Telangana';
ld.Country = 'India';

lstLeads.Add(ld);

// Second Lead Record..


Lead lds = new Lead();

lds.FirstName = 'Srinivas';
lds.LastName = 'SFDC';
lds.Title = 'Technical Lead';
lds.Company = 'IBM Inc.';
lds.Status = 'Open - Not Contacted';
lds.Rating = 'Hot';
lds.Industry = 'Manufacturing';
lds.AnnualRevenue = 2500000;
lds.Phone = '9900998877';
lds.Fax = '7788990099';
lds.Email = 'sfdcvaasu@gmail.com';
lds.LeadSource = 'Email';
lds.City = 'Bangalore';
lds.State = 'Karnataka';
lds.Country = 'India';

lstLeads.Add(lds);

// Insert the Lead Records..


if(! lstLeads.isEmpty())
{
Insert lstLeads;

// Call the Method to Send the Email Notifications..


MessagingUtility.SendEmailNotificationToLeads(lstLeads);
}
}
}

MessagingUtility Class:
-----------------------
public class MessagingUtility
{
Public static void SendEmailNotificationToLeads(List<Lead> leadRecords)
{
if(! leadRecords.isEmpty())
{
// Create a List Collection.To Store multiple Lead Record's Email
Notification Details..
List<Messaging.SingleEmailMessage> lstEmails = new
List<Messaging.SingleEmailMessage>();

for(Lead ld : leadRecords)
{
Messaging.SingleEmailMessage email = new
Messaging.SingleEmailMessage();

string[] toAddress = new string[]{ld.Email,


'ferozjani@gmail.com'};
email.setToAddresses(toAddress);

string[] ccAddress = new string[]{ld.Email,


'ferozjani@gmail.com'};
//email.setCcAddresses(ccAddress);

string[] bccAddress = new string[]{ld.Email,


'ferozjani@gmail.com'};
//email.setBccAddresses(bccAddress);

email.setReplyTo('customersupport@icici.com');

email.setSenderDisplayName('ICICI Bank Credit Card Department.');

string emailSubject = 'Congratulations '+ ld.FirstName + ' '+


ld.LastName + ' ..!! Your Credit Card Application has been received Successfully.';
email.setSubject(emailSubject);

string emailContent = 'Dear '+ ld.FirstName + ' '+ ld.LastName+


', <br/><br/>'+
'Thanks for showing your interest in our
Organization. And we are pleased to inform you your Credit Card Application has
been received Successfully. <br/><br/>'+
'Here are your Application Details...: <br/><br/>'+
'Application ID ....: '+ ld.Id +
'<br/> Applicant Name : '+ ld.FirstName + ' '+
ld.LastName+
'<br/> Applicant Designation ....: '+ ld.Title +
'<br/> Company Name .....: '+ ld.Company +
'<br/> Application Status .....: '+ ld.Status +
'<br/> Contact Number .......: '+ ld.Phone +
'<br/> Fax Number .........: '+ ld.Fax +
'<br/> Applicant Email Id .....: '+ ld.Email+
'<br/> Applicant Rating ........: '+ ld.Rating +
'<br/> Industry Name .........: '+ ld.Industry+
'<br/> Annual Revenue is.........: '+
ld.AnnualRevenue +
'<br/> Lead Source ............: '+ ld.LeadSource+
'<br/> Applicant City ..........: '+ ld.City+
'<br/> Applicant State Name ......: '+ ld.State+
'<br/> Country Name .............: '+ ld.Country+
'<br/><br/> One of our Sales Person will contact you
Shortly. Please contact on the below address, if any queries.<br/><br/>'+
'Thanks & Regards, <br/> ICICI Bank Credit Card
Department, <br/> Hyderabad.';

email.setHtmlBody(emailContent);

// Preparing Attachment...
Messaging.EmailFileAttachment attach = new
Messaging.EmailFileAttachment();
attach.setContentType('Application/pdf');
attach.setFileName(ld.FirstName+' '+ld.LastName+'.pdf');
attach.body = Blob.toPdf(emailContent);

//Prepare a List Collection to Store all the Attachments..


List<Messaging.EmailFileAttachment> lstAttachments = new
List<Messaging.EmailFileAttachment>();

// Add the Attachment to List Collection..


lstAttachments.Add(attach);

// Add the Attachments to Email..


email.setFileAttachments(lstAttachments);

// Add the Lead Email Alert to Collection..


lstEmails.Add(email);
}

if(! lstEmails.isEmpty())
{
Messaging.SendEmailResult[] results =
Messaging.sendEmail(lstEmails);
}
}
}
}

Execution:
----------
LeadHandler.CreateLeadRecords();

list<candidate_registration_forms__c> CA = new
list<candidate_registration_forms__c>{};

candidate_registration_forms__c C1 = new candidate_registration_forms__c();


C1.first_Name__c = 'Aniket 4';
C1.last_name__c = 'SOMVANSHI';
C1.highest_qualification__c = 'graduate';
C1.qualification__c = 'BE';
CA.add(C1);
candidate_registration_forms__c C2 = new candidate_registration_forms__c();
C2.first_Name__c = 'Arvind 21';
C2.last_name__c = 'KEJRIWAL';
C2.highest_qualification__c = 'graduate';
C2.qualification__c = 'Btech';
CA.add(C2);

candidate_registration_forms__c C3 = new candidate_registration_forms__c();


C3.first_Name__c = 'Narendra 323';
C3.last_name__c = 'MODI';
C3.highest_qualification__c = 'graduate';
C3.qualification__c = 'BE';
CA.add(C3);

candidate_registration_forms__c C4 = new candidate_registration_forms__c();


C4.first_Name__c = 'Rashmi 432';
C4.last_name__c = 'SINGH';
C4.highest_qualification__c = 'graduate';
C4.qualification__c = 'BA';
CA.add(C4);

candidate_registration_forms__c C5 = new candidate_registration_forms__c();


C5.first_Name__c = 'Amit 532';
C5.last_name__c = 'SHAH';
C5.highest_qualification__c = 'graduate';
C5.qualification__c = 'BE';
CA.add(C5);

insert CA;

list<candidate_registration_forms__c> CAND = [select


Id,first_Name__c,qualification__c,preferred_country__c FROM
candidate_registration_forms__c WHERE qualification__c='BE' ];
list<candidate_registration_forms__c> CANDnew = new
list<candidate_registration_forms__c>();
FOR(candidate_registration_forms__c EC:CAND)
{
if(EC.qualification__c == 'BE')
{
EC.preferred_country__c = 'AUS';
CANDnew.add(EC);
}
}
UPDATE CANDnew;

list<Account> SFDCACC = [select Name,(SELECT Name,Accountid FROM contacts) from


Account Where Name = 'SFDC Account'];
for(Account EA:SFDCACC)
{
EA.Industry = 'Banking';
EA.Phone = '197279';
EA.Rating = 'Hot';
Update EA;
for(contact EC:EA.contacts)
{
EC.Department = 'SFDC';
Update EC;
}
}

list<contact> CONS = [select ID,name,Phone from Contact where Email = null];


list<Contact> CONnew = new list <contact>{};
for (contact EA: CONS)
{
EA.Phone = '13142';
CONnew.add(EA);

}
database.SaveResult[] result = database.insert(CONnew, false);

list<contact> CONS = [select ID,name,Phone from Contact where Email = null];


list<Contact> CONnew = new list <contact>{};
for (contact EA: CONS)
{
EA.Phone = '13142';
CONnew.add(EA);

}
database.SaveResult[] result = database.update(CONnew, false);
System.debug(CONS.size());

You might also like