Table of Contents
Introduction to Apex Scenario
Develop an Apex Trigger to send the email to the Account owner when the Account is Created within Salesforce and Account does not have the phone and industry field populated.
Also, create a task under the account with the following information
- Subject – The phone and Industry of the Account are Blank
- Description – The phone and Industry of the Account are Blank
- Priority – High
- Status – Not Started
- What Id – Related Account Id
- OwnerId – Related Account Owner
Email Template
Dear <Account Owner>
A new account <account name> has been created in Salesforce. The account is missing some important information Phone & Industry.
Please try to collect this information and update the account ASAP.
Thanks & Regards,
<Company Name>
Questions to be asked
- Which Object?
- Account
- Event?
- after insert
- Requirement?
- Send an Email & Create a Task
Hands-On
Email Utility Class
public class EmailUtility {
public static void sendEmail(){
// To Address
// Body (Content)
// Subject
// CC Address
// BCC Address
// Attachments (Files)
// Messaging
/* Step1 - Create the object of SingleEmailMessage */
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('My First Email From Salesforce');
// Images/Buttons/Links/Bold/Color - HTML Body
// Simple Text - Plain Text Body
//email.plaintextbody = '';
//email.toaddresses = '';
List toAddress = new List();
toAddress.add('sfdcpanther@gmail.com');
toAddress.add('sfdcpanther@gmail.co');
List bccAddress = new List();
bccAddress.add('contentlearning222@gmail.com');
email.setPlainTextBody('Hello Dear, My First Email From Salesforce!');
email.setToAddresses(toAddress);
email.setBccAddresses(bccAddress);
List emailMessages = new List();
emailMessages.add(email);
List sendEmailResults = Messaging.sendEmail(emailMessages, false);
for(Messaging.SendEmailResult sr: sendEmailResults){ // 10 Emails - 8 Success, 2 Fail
Boolean isSuccess = sr.isSuccess(); // True OR False
if(isSuccess){
System.debug('Email Sent Successfully!!');
}else{
System.debug('Error while sending Email \n ');
List errors = sr.getErrors();
System.debug(errors);
}
}
//public Messaging.SendEmailResult[] sendEmail(Messaging.Email[] emails, Boolean allOrNothing)
}
/*public class Messaging{
public class SingleEmailMessage{
}
}*/
}
Handler Class
public class AccountTriggerHandler {
// before insert
public static void handleBeforeInsert(List accountList){
AccountTriggerHelper.updateAccountShippingAddress(accountList);
AccountTriggerHelper.checkDuplicateAccount(accountList);
}
// before update
public static void handleBeforeUpdate(List accountList){
AccountTriggerHelper.updateAccountShippingAddress(accountList);
AccountTriggerHelper.checkDuplicateAccount(accountList);
}
//After insert
public static void handleAfterInsert(List accountList){
/*
When the Account is Created,
Create a Task Record under that Account and assign the Task to the Account Owner.
Use the below information
* Subject - Created from Apex Trigger
* Description - Created from Apex Trigger
* Due Date - Todays Date + 7
* Status - Not Started
* Priority - High
* OwnerId - Account OwnerId
* WhatId - Account.Id
*/
/* Sub-Problems */
// 1. Check if the code is running after insert
// Do not make any DML withing for loop ( LIMIT - 150 ~ 151 )
// Do not make any SOQL withing for loop ( LIMIT - 100 ~ 101 )
List taskRecordToInsertList = new List();
List emailMessages = new List();
List newAccountList = [SELECT Id, Name, Phone, Industry, OwnerId, Owner.Name, Owner.Email FROM Account WHERE Id IN: accountList];
for(Account acc : newAccountList){ // Trigger.size = 200
// Prepare Task Record
if(acc.Phone == null && acc.Industry == null){ // 20
// Requirement #1 - Create Task
Task t = new Task();
t.Subject = 'Created from Apex Trigger';
t.Description = 'Created from Apex Trigger';
t.Status = 'Not Started';
t.Priority = 'High';
t.ActivityDate = System.today().addDays(7);
t.WhatId = acc.Id;
t.OwnerId = acc.OwnerId;
taskRecordToInsertList.add(t);
// Requirement #2 - Send Email
// Prepare Email - DRAFT Email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('New Account '+acc.Name+' has been assigned!');
// OwnerId (User/Queue)
String emailBody = 'Dear '+acc.Owner.Name+'
';
emailBody += 'A new account '+acc.Name+' has been created in Salesforce. The account is missing some important information Phone & Industry.
';
emailBody += 'Please try to collect this information and update the account ASAP.
';
emailBody += 'Thanks & Regards,
';
emailBody += 'Panther Schools';
//email.setPlainTextBody(emailBody);
email.setHtmlBody(emailBody);
List toAddress = new List();
// Lead/Contact/User - Email/Id
toAddress.add(acc.OwnerId); // OwnerEmail
email.setToAddresses(toAddress);
List bccAddress = new List();
bccAddress.add('contentlearning222@gmail.com');
email.setBccAddresses(bccAddress);
// Prepare the Attachment and send along with the email
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName('Panther-Schools.txt'); // pdf, txt, png, jpg, jpeg, word, zip
String strBody = 'This is simple text attached from Salesforce';
Blob textBody = Blob.valueOf(strBody);
attach.setBody(textBody);
List fileAttachments = new List();
fileAttachments.add(attach);
email.setFileAttachments( fileAttachments );
email.setReplyTo('no-reply@gmail.com');
email.setSenderDisplayName('Amit From Panther Schools');
// Add Email to the List
emailMessages.add(email);
}
}
// insert Task Record
insert taskRecordToInsertList;
// 200 times
// Send Email
List sendEmailResults = Messaging.sendEmail(emailMessages, false);
for(Messaging.SendEmailResult sr: sendEmailResults){ // 10 Emails - 8 Success, 2 Fail
Boolean isSuccess = sr.isSuccess(); // True OR False
if(isSuccess){
System.debug('Email Sent Successfully!!');
}else{
System.debug('Error while sending Email \n ');
List errors = sr.getErrors();
System.debug(errors);
}
}
}
// after the update
public static void handleAfterUpdate(){
}
}
Watch Complete Video
Resources
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_single.htm#apex_classes_outbound_single
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_messaging.htm#apex_System_Messaging_sendEmail
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_sendemailresult.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_attachment.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_sendemailerror.htm
- https://www.pantherschools.com/category/developer/