-
Salesforce Batch Apex - send Leads and opportunity records in one email along with two different tables.
Actually, I write a batch class to send an email last month leads to the loan officer lookup relation with a contact object at the beginning of the new month in a table format. Now I wanna send lead and opportunity both in one email along with two different tables. I have tried too many time but I could not succeed. Help me how to send two objects data in an email. Here is my existing code
global class testoppty implements Database.Batchable<sObject> { public String query = 'SELECT Loan_Officer_1a__c,Loan_Officer_1a__r.Email, Name, Phone, Starting_Credit_Score__c, ' + 'Status, Enrolled_On__c, Est_Re_Pull_Date__c, Realtor_Name__c ' + ' FROM Lead'; query = 'SELECT Loan_Officer_1__c,Loan_Officer_1__r.Email, Name, Starting_Credit_Score__c, ' + ' StageName, Enrolled_On__c, Est_Re_Pull_Date__c, Realtor_ID__r.Name ' + ' FROM Opportunity'; public EmailTemplate templateId = [Select Id,HtmlValue,Subject from EmailTemplate where name = 'LoanOfficerRecord' LIMIT 1]; global Database.QueryLocator start(Database.BatchableContext bc) { query += ' WHERE CreatedDate >= LAST_MONTH AND CreatedDate <= THIS_MONTH AND Loan_Officer_1a__c != null'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list<Lead> allLeads) { Map<Id,List<Lead>> leadMap = new Map<Id,List<Lead>>(); List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMEssage>(); if(allLeads != null && allLeads.size() > 0){ for(Lead l: allLeads){ if(!leadMap.containsKey(l.Loan_Officer_1a__c)){ leadMap.put(l.Loan_Officer_1a__c, new List<lead>()); } leadMap.get(l.Loan_Officer_1a__c).add(l); } } if(leadMap.keySet().size() > 0){ Map<Id,Contact> officers = new Map<Id,Contact>([SELECT Id,Email,Name FROM Contact WHERE Id IN: leadMap.keySet()]); for(Id i: leadMap.keySet()){ Contact con = officers.get(i); System.debug(con); if(String.isnOtBlank(con.Email)){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[]{con.EMail}); mail.setSubject(templateId.Subject); String html = templateId.HtmlValue; html = html.replace('||OfficerName||',con.Name); String leadsTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+ '<tr style="font-weight:bold;"><td>Name</td><td>Phone</td><td>Starting Credit Score</td><td>Status</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>'; for(Lead l: leadMap.get(i)){ leadsTable += '<tr><td>'+l.Name+'</td>'+ '<td>'+l.Phone+'</td><td>'+l.Starting_Credit_Score__c+'</td><td>'+l.Status+'</td><td>'+l.Enrolled_On__c+'</td>'+ '<td>'+l.Est_Re_Pull_Date__c+'</td><td>'+l.Realtor_Name__c+'</td></tr>'; } leadsTable += '</table>'; html = html.replace('||Leads||',leadsTable); html = html.replace('null',' '); mail.setHTMLBody(html); mails.add(mail); } } } if(mails.size() > 0){ Messaging.sendEmail(mails); } } global void finish(Database.BatchableContext BC) { } }-
This discussion was modified 7 years ago by
Muhammad Jawwad.
-
This discussion was modified 7 years ago by
Forcetalks.
-
This discussion was modified 6 years, 9 months ago by
Forcetalks.
-
This discussion was modified 6 years, 9 months ago by
Forcetalks.
-
This discussion was modified 7 years ago by
Log In to reply.
Popular Salesforce Blogs
Why Salesforce Lightning is the Key to Unlocking Your Sales Potential
Salesforce is the world's leading customer relationship management (CRM) platform, enabling businesses to manage their customer interactions, streamline their operations, and increase their revenue. With…
Related Records Updation with the Help of Salesforce Flow
Hello all, In this blog, we will get to know how to update records using Salesforce flow. For updating the related object records we can…
Top 10 Features of Code Completion for Lightning in JetForcer IDE
Today, I would like to tell you about the top 10 code completion features in JetForcer IDE, which indeed save time during developing Lightning applications.…
Popular Salesforce Videos
DreamTX Welcome Day 1 | Dreamforce 2020 | Salesforce
If you missed DreamTX DAY 1, then live this amazing experience here, we have covered it all for you. Let’s come together for the Dreamforce…
UiPath automation for Salesforce | REST API commands | OAuth2 | Download files | Without activity
How to send a raw REST API command from UiPath to Salesforce without using custom activities. 0:00 Intro 1:35 OAuth2 4:10 Download files from a case, opportunity, account,…
Salesforce Acquires Slack
When Salesforce bought Slack earlier this week for $27.7 billion, it was in some ways the end of a startup fairytale. Slack was the living…