Tag Archives: Email

Send Email To Non-Contacts Using Apex

Using renderStoredEmailTemplate(templateId, whoId, whatId) method of Messaging Class, we can send email to non-contacts using apex. Here is an example to send email to Opportunity owner.

Sample code:

//Send email to opportunity owner
Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(emailTemplateId, ownerId, opportunityId);
mail.setTargetObjectId(ownerId);
mail.setSubject(mail.getSubject());
mail.sethtmlBody(mail.gethtmlBody());
mail.saveAsActivity = false;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

Quick Text in Salesforce

Quick text is predefined messages, like greetings, answers to common questions, and short notes. Which helps to stop retyping the same message over and over and save time. You can insert quick text in emails, chats, and more.

Enable Quick Text:

  • From Setup enter Quick Text Settings in the Quick Find box
  • Select Quick Text Settings
  • Click Enable Quick Text
  • Save

Give Users Access to Quick Text:
Giving users access to quick text lets them insert predefined messages in their emails, chats, events, tasks, Knowledge articles, and more. Service agents can respond to customers and update cases quickly and easily. Sales reps can work with their contacts, accounts, and opportunities more efficiently.

Use a permission set or update profiles to give your users Read permission on the Quick Text object. Optionally, you can also give users Create, Edit, and Delete access to let them create and manage their own quick text messages.

Create Quick Text Messages:
Create custom predefined messages to insert into emails, chats, tasks, events, and more. Quick text can include merge fields, line breaks, and special characters.

  • Open Quick Text tab
  • Click on New to create Quick Text (You can create record type for Quick Text)
  • Enter a message name (Use a name that helps users identify when to use this message)
  • Enter the message (The message can include line breaks, lists, special characters, merge fields, and up to 4,000 characters)
  • You can merge fields based on your requirement.
  • Select the channels in which you want the message to be available.
    • Email : For Email actions
    • Event : For Event actions
    • Internal : Works with internal fields, like on the Change Status action
    • Knowledge : For Knowledge articles in Lightning Experience
    • Live Agent : Works with Live Agent chat in the Service Console
    • Phone : or the Log a Call action
    • Portal : Works in a community or a customer portal
    • Social : For social posts
    • Task : For Task actions
  • Select a category
  • Select a channel
  • If you use merge fields, click Preview to review the message with data from records that you choose.
  • Save

Insert and Use Quick Text:
You can use quick text on all standard and custom objects in the following supported quick actions or places: emails, events, Knowledge articles, Live Agent chats, Log a Call actions, social posts, and tasks.

Share Quick Text:
You can share quick text with users, public groups, and more. The way you share quick text in Salesforce Classic and Lightning Experience is different. In Salesforce Classic, you can share individual quick text. In Lightning Experience, you share quick text using folders.

You can also change your org-wide default sharing setting for quick text. Or you can limit access by creating sharing rules to specify which groups of users have access to quick text.

Send Email With Document As An Attachment Using Apex In Salesforce

Sample Code Approach 1:

//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];

//Create Email file attachment from document
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType(doc.ContentType);
attach.setFileName(doc.DeveloperName+'.'+doc.Type);
attach.setInline(false);
attach.Body = doc.Body;

//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });//Set File Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email

Sample Code Approach 2:

//Get your document from document Object
Document doc = [Select Id, Name, Body, ContentType, DeveloperName, Type From Document Where DeveloperName = 'Your_Doucment_DeveloperName'];

//Apex Single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { 'itzbiswajeet@gmail.com' });//Set To Email Address
mail.setSubject('Test Email With Attachment');//Set Subject
mail.setHtmlBody('Please find the attachment.');//Set HTML Body
mail.setDocumentAttachments(new Id[]{doc.Id});//Set Document Attachment
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });//Send Email

Send Meeting Invite Calendar Email From Apex

Sample Code:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Set To Addresss
mail.setToAddresses(new List<String>{'itzbiswajeet@gmail.com'});
//Set Email Subject
mail.setSubject('Test Meeting Invitation');
//Set Email Body
mail.setPlainTextBody('Test Meeting');

//Meeting Start Time & End Time
DateTime dt = DateTime.now().addDays(5);
//Start Date
String startDT = String.valueof(dt.year() +'0'+ dt.month() +''+ dt.day() + 'T010000Z');
//End Date
String endDT = String.valueof(dt.year() +'0'+ dt.month() +''+ dt.day() + 'T020000Z');

//Create Meeting Body
String meetingInviteBody = ''; 
meetingInviteBody += 'BEGIN:VCALENDAR\n';        
meetingInviteBody += 'PRODID::-//hacksw/handcal//NONSGML v1.0//EN\n';
meetingInviteBody += 'VERSION:2.0\n';
meetingInviteBody += 'METHOD:PUBLISH\n';
meetingInviteBody += 'X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n';
meetingInviteBody += 'BEGIN:VEVENT\n';
meetingInviteBody += 'CLASS:PUBLIC\n';
meetingInviteBody += 'CREATED:20150126T203709Z\n';        
meetingInviteBody += 'DTEND:'+endDT+'\n';
meetingInviteBody += 'DTSTAMP:20150126T203709Z\n';        
meetingInviteBody += 'DTSTART:'+startDT+'\n';
meetingInviteBody += 'LAST-MODIFIED:20150126T203709Z\n';
meetingInviteBody += 'LOCATION:USA\n';
meetingInviteBody += 'PRIORITY:5\n';
meetingInviteBody += 'SEQUENCE:0\n';
meetingInviteBody += 'SUMMARY:Test Meeting\n';
meetingInviteBody += 'LANGUAGE=en-us:Meeting\n';
meetingInviteBody += 'TRANSP:OPAQUE\n';
meetingInviteBody += 'X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><META NAME="Generator" CONTENT="MS Exchange Server version 08.00.0681.000"><TITLE></TITLE></HEAD><BODY><!-- Converted from text/plain format --></BODY></HTML>\n';
meetingInviteBody += 'X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n';
meetingInviteBody += 'X-MICROSOFT-CDO-IMPORTANCE:1\n';
meetingInviteBody += 'END:VEVENT\n';
meetingInviteBody += 'END:VCALENDAR';

//Meeting Email Attachment
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.Filename = 'meeting.ics'; 
attach.ContentType = 'text/calendar';     
attach.Inline = true;     
attach.Body = Blob.valueOf(meetingInviteBody);

//Attach Meeting Attachment
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
//Send Email
Messaging.SendEmailResult[] er = Messaging.sendEmail(new Messaging.Email[] { mail });