In this Training, you create an EmailManager class that encapsulates the logic to send confirmation emails to the conference speakers.
Step 1: Create the EmailManager class
1.Click and select Developer Console.
2.In the Developer Console, click File > New > Apex Class. Specify EmailManager
as the class name and click OK.
3.Paste the following into the Developer Console.
public with sharing class EmailManager{
public void sendMail(String [] addresses, String [] subjects, String [] messages) {
Messaging.SingleEmailMessage [] emails = new Messaging.SingleEmailMessage[]{};
Integer totalMails = addresses.size();
for(Integer i=0; i < totalMails; i++){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject(subjects[i]);
email.setToAddresses(new List<String> { addresses[i] });
email.setPlainTextBody(messages[i]);
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
4.Click File > Save to save the file.
Step 2: Send an Email
In this step, you test the EmailManager
class by sending an email from the Developer Console. Later in this project, you integrate the EmailManager
class with other parts of the application to automate the process of sending confirmation emails.
1.In the Developer Console, click Debug > Open Execute Anonymous Window.
2.Type the following Apex code (provide your own email address).
String address = ‘YOUR_EMAIL_ADDRESS’;
String subject = ‘Speaker Confirmation’;
String body = ‘Thank you for speaking at the conference.’;
String[] addresses = new String[]{},
subjects = new String[]{},
messages = new String[]{};
addresses.add(address);
subjects.add(subject);
messages.add(body);
EmailManager em = new EmailManager();
em.sendMail(addresses, subjects, messages);
4.Click the Execute button.
5.Check your email. You should have received a confirmation email.
Step 3: Using a Static Method
Since EmailManager
is a utility class that doesn’t work with instance-specific variables, you can make the sendMail()
method static.
1.In the Developer Console, open the EmailManager
class.
2.To turn sendMail()
into a static method, change its signature as follows (add the static
keyword).
public static void sendMail(String[] addresses, String[] subjects, String[] messages) {
3.Save the file.
4.Go back to the Execute Anonymous Window (Debug > Open Execute Anonymous Window).
5.Modify the Apex code to invoke sendMail() statically (provide your own email address).
String address = ‘YOUR_EMAIL_ADDRESS’;
String subject = ‘Speaker Confirmation’;
String body = ‘Thank you for speaking at the conference.’;
String[] addresses = new String[]{},
subjects = new String[]{},
messages = new String[]{};
addresses.add(address);
subjects.add(subject);
messages.add(body);
EmailManager.sendMail(addresses, subjects, messages);
Click Execute and check your email.
Please refer the following link to learn more about Apex class programming How to get geolocation through apex class
We are the ISV Partners and Please reach us for custom development => www.merfantz.com
This training will be very useful to learn about Apex class creation in salesforce.