top of page



APEX Code to Get the List of leads and Send Email #SFDC

Here is an example of an Apex code that you can use to list the leads in a tabular format and send them in an email body:

List<Lead> leads = [SELECT Id, FirstName, LastName, Company, Status FROM Lead];

String emailBody = '';

// Create the table header
emailBody += '<table><tr><th>Name</th><th>Company</th><th>Status</th></tr>';

// Iterate over the leads and add a row to the table for each lead
for (Lead lead : leads) {
    emailBody += '<tr><td>' + lead.FirstName + ' ' + lead.LastName + '</td><td>' + lead.Company + '</td><td>' + lead.Status + '</td></tr>';

// Close the table
emailBody += '</table>';

// Send the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {''});
email.setSubject('Leads List');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });

This code will execute a SOQL query to retrieve all leads from the Lead object in Salesforce. It will then create an HTML table in the email body, with a header row that includes the column names "Name", "Company", and "Status".

The code will then iterate over the list of leads and add a row to the table for each lead, including the first and last name, company, and status of the lead.

Finally, the code will send an email with the subject "Leads List" and

you can see that the the email is received where the Leads are Shown in the email body.

Hope This Helps .

Happy Learning !

7 views0 comments

Recent Posts

See All

A Scratch org is a disposable, flexible, and completely isolated Salesforce environment that enables developers to test and experiment with new features and customizations without affecting the produc

Salesforce DX (Development Experience) is a set of tools and processes given by Salesforce for modern Salesforce application development and delivery. It offers a streamlined development method for de

Other Posts you may Interested 

bottom of page