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. [Link] Class
2. [Link] Class
Inbound Email Features: (Receive the Emails)
-----------------------
Provides an Interface as below.
1. [Link] Interface
|
--> HandleInboundEmail()
Method
[Link] Class:
===================================
Syntax:
[Link] <objectName> = new
[Link]();
Ex:
[Link] email = new [Link]();
Methods:
--------
1. SetToAddresses(<List / Array>):
Ex:
string[] toAddress = new string[]
{'[Link]@[Link]','ram@[Link]'};
[Link](toAddress);
2. SetCCAddresses(<List / Array Name>):
Ex:
string[] ccAddress = new string[]
{'[Link]@[Link]','ram@[Link]'};
[Link](ccAddress);
3. SetBCCAddresses(<List / Array Name>):
Ex:
string[] bccAddress = new string[]
{'[Link]@[Link]','ram@[Link]'};
[Link](bccAddress);
Note:
Total Number of email id's including "To Address, CC Address, BCC
Address" is max. of 150.
4. SetReplyTo(<String replyToEmailID>):
Ex:
[Link]('customersupport@[Link]');
5. SetSenderDisplayName(String senderDisplayName):
Ex:
[Link]('ICICI Bank Credit Card Department');
6. SetSubject(String emailSubject):
Ex:
[Link]('Email Subject');
7. SetPlainTextBody(string emailContent):
Ex:
[Link]('Plain Text form of Email Content');
8. SetHTMLBody(string htmlEmailContent):
Ex:
[Link]('HTML Email Content');
9. SetFileAttachments(List<EmailFileAttachment> lstAttachments):
Ex:
[Link](<List Collection>):
Send the Email:
---------------
Syntax:
[Link][] results =
[Link](List<[Link]>
lstEmails);
(OR)
[Link][] results = [Link](new
[Link][]{<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();
[Link] = 'Archana';
[Link] = 'Prakash';
[Link] = 'Project Manager';
[Link] = 'Cognizant Technology.';
[Link] = 'Open - Not Contacted';
[Link] = 'Hot';
[Link] = 'Technology';
[Link] = 3700000;
[Link] = '9900887766';
[Link] = '8899887766';
[Link] = 'darchana214@[Link]';
[Link] = 'Web';
[Link] = 'Hyderabad';
[Link] = 'Telangana';
[Link] = 'India';
[Link](ld);
// Second Lead Record..
Lead lds = new Lead();
[Link] = 'Srinivas';
[Link] = 'SFDC';
[Link] = 'Technical Lead';
[Link] = 'IBM Inc.';
[Link] = 'Open - Not Contacted';
[Link] = 'Hot';
[Link] = 'Manufacturing';
[Link] = 2500000;
[Link] = '9900998877';
[Link] = '7788990099';
[Link] = 'sfdcvaasu@[Link]';
[Link] = 'Email';
[Link] = 'Bangalore';
[Link] = 'Karnataka';
[Link] = 'India';
[Link](lds);
// Insert the Lead Records..
if(! [Link]())
{
Insert lstLeads;
// Call the Method to Send the Email Notifications..
[Link](lstLeads);
}
}
}
MessagingUtility Class:
-----------------------
public class MessagingUtility
{
Public static void SendEmailNotificationToLeads(List<Lead> leadRecords)
{
if(! [Link]())
{
// Create a List [Link] Store multiple Lead Record's Email
Notification Details..
List<[Link]> lstEmails = new
List<[Link]>();
for(Lead ld : leadRecords)
{
[Link] email = new
[Link]();
string[] toAddress = new string[]{[Link],
'ferozjani@[Link]'};
[Link](toAddress);
string[] ccAddress = new string[]{[Link],
'ferozjani@[Link]'};
//[Link](ccAddress);
string[] bccAddress = new string[]{[Link],
'ferozjani@[Link]'};
//[Link](bccAddress);
[Link]('customersupport@[Link]');
[Link]('ICICI Bank Credit Card Department.');
string emailSubject = 'Congratulations '+ [Link] + ' '+
[Link] + ' ..!! Your Credit Card Application has been received Successfully.';
[Link](emailSubject);
string emailContent = 'Dear '+ [Link] + ' '+ [Link]+
', <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 ....: '+ [Link] +
'<br/> Applicant Name : '+ [Link] + ' '+
[Link]+
'<br/> Applicant Designation ....: '+ [Link] +
'<br/> Company Name .....: '+ [Link] +
'<br/> Application Status .....: '+ [Link] +
'<br/> Contact Number .......: '+ [Link] +
'<br/> Fax Number .........: '+ [Link] +
'<br/> Applicant Email Id .....: '+ [Link]+
'<br/> Applicant Rating ........: '+ [Link] +
'<br/> Industry Name .........: '+ [Link]+
'<br/> Annual Revenue is.........: '+
[Link] +
'<br/> Lead Source ............: '+ [Link]+
'<br/> Applicant City ..........: '+ [Link]+
'<br/> Applicant State Name ......: '+ [Link]+
'<br/> Country Name .............: '+ [Link]+
'<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.';
[Link](emailContent);
// Preparing Attachment...
[Link] attach = new
[Link]();
[Link]('Application/pdf');
[Link]([Link]+' '+[Link]+'.pdf');
[Link] = [Link](emailContent);
//Prepare a List Collection to Store all the Attachments..
List<[Link]> lstAttachments = new
List<[Link]>();
// Add the Attachment to List Collection..
[Link](attach);
// Add the Attachments to Email..
[Link](lstAttachments);
// Add the Lead Email Alert to Collection..
[Link](email);
}
if(! [Link]())
{
[Link][] results =
[Link](lstEmails);
}
}
}
}
Execution:
----------
[Link]();
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';
[Link](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';
[Link](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';
[Link](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';
[Link](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';
[Link](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';
[Link](EC);
}
}
UPDATE CANDnew;
list<Account> SFDCACC = [select Name,(SELECT Name,Accountid FROM contacts) from
Account Where Name = 'SFDC Account'];
for(Account EA:SFDCACC)
{
[Link] = 'Banking';
[Link] = '197279';
[Link] = 'Hot';
Update EA;
for(contact EC:[Link])
{
[Link] = '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)
{
[Link] = '13142';
[Link](EA);
}
[Link][] result = [Link](CONnew, false);
list<contact> CONS = [select ID,name,Phone from Contact where Email = null];
list<Contact> CONnew = new list <contact>{};
for (contact EA: CONS)
{
[Link] = '13142';
[Link](EA);
}
[Link][] result = [Link](CONnew, false);
[Link]([Link]());