Introduction:
Building strong relationships with leads is crucial for business growth, and Salesforce provides powerful tools to streamline lead management. One key aspect is engaging leads through timely and personalized welcome emails. In this blog post, we will explore a solution that automatically sends a welcome email to a lead one day after they are added as a campaign member to a specific campaign. We will walk you through the implementation steps using Salesforce Scheduled Flow and Apex batch processing, along with a sample scheduled flow, Apex batch code, and email template.
Figure 1: Flow for implementing Lead Campaign Email
Figure 2: Setting Entry Conditions - CampaignId IsNull False, LeadId isNull False, Status Equals Sent
Figure 3: Setting Field Details To Send Email In The Flow
Figure 4: Adding Lead to Campaign Member
Figure 5: Welcome Email is Getting Generated Through Flow
Step 2: Creating the Apex Batch Class
Apex Batch Code
global class LeadcampaignEmail implements Database.Batchable {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, LeadId, Lead.Email, Status, CampaignId FROM CampaignMember';
System.debug('query'+query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List cmList) {
List emailsToSend = new List();
System.debug('cmlist'+cmlist);
List Mails = new List();
for (CampaignMember cm : cmList) {
if((cm.LeadId != Null)&&(cm.Status == 'Sent')){
Mails.add(cm.Lead.Email);
}
}
if(!Mails.isEmpty()){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(Mails);
email.setSubject('Lead Test Mail');
email.setPlainTextBody('Olaa! You have been added to Campaign Member.');
emailsToSend.add(email);
Messaging.SendEmailResult[] emailResults = Messaging.sendEmail(emailsToSend);
System.debug('Email Sent Successfully');
}
}
global void finish(Database.BatchableContext BC) {
System.debug('Finished');
}
}
Figure 1: Adding Lead to Campaign Member
Figure 2: Welcome Email is Getting Generated Through Batch Apex Class
Conclusion:
Automating the process of sending welcome emails to leads one day after they are added as campaign members in Salesforce can significantly enhance lead engagement and streamline lead management processes. By following the steps outlined in this blog post, you can implement this solution using Salesforce Scheduled Flow and Apex batch processing. Additionally, we have discussed the importance of creating a well-designed email template to make a positive first impression on your leads.
Remember to adapt the scheduled flow, Apex batch class, and email template according to your specific campaign and lead criteria, branding guidelines, and preferences. Salesforce's flexibility allows for customization and tailoring to meet your organization's unique needs. Embrace the power of automation and leverage Salesforce's capabilities to enhance your lead engagement strategies and drive business growth.
For any queries please reach out tosupport@astreait.com.