When working with Salesforce data, it’s common to pull related information across objects. One useful scenario is retrieving the email addresses of all contacts related to a specific Account maybe to send notifications, reports, or alerts.
In this blog post, we’ll write a simple Apex method that does exactly that. It takes an Account’s name, queries all Contacts under it, and returns a list of their email addresses.
If you’re new to Apex or want a real-world example of using SOQL (Salesforce Object Query Language), this one’s for you!
🧠 What Are We Trying to Do?
Let’s say you have a customer account named “Acme Inc”, and you want to get the email addresses of all contacts who work for them.
We’re going to:
Accept an account name as input.
Query all Contact records related to that account.
Collect all the non-null email addresses.
Return them in a List<String>.
This kind of logic is great for background processing, sending emails, or even building custom reports.
🔍 Code Explanation
public with sharing class ApexUseCaseFour {
public static List<String> returnContactEmails(String accountName){
We start by defining a class ApexUseCaseFour
with a static method returnContactEmails
. It accepts the Account’s name as a String
.
List<String> emailList = new List<String>();
We create an empty list that will store all email addresses we collect.
List<Contact> contactRecords = [SELECT Id, Email FROM Contact WHERE Account.Name = :accountName];
This is the key part: we run a SOQL query to fetch all Contacts where the Account’s Name matches the input. We’re selecting only the fields we need: Id
and Email
.
💡 Note: We’re using a relationship field here:
Account.Name
— this is how we filter child records (Contact
) by a parent field (Account.Name
).
if(contactRecords.size() > 0){
for(Contact con : contactRecords){
if(con.Email != null){
emailList.add(con.Email);
}
}
}
We loop through the result set. For each contact, we check if the email is not null and then add it to our list.
return emailList;
}
}
At the end, we return the list of collected email addresses.
✅ Why This Is Useful
This method is super helpful in real-world scenarios like:
Sending email alerts to everyone associated with a particular customer.
Generating reports based on active client contacts.
Automating communication via Apex.
It also introduces a basic but powerful concept: querying related records using SOQL something you’ll use all the time as a Salesforce developer.
🧾 Final Code Snippet
public with sharing class ApexUseCaseFour {
public static List<String> returnContactEmails(String accountName){
List<String> emailList = new List<String>();
List<Contact> contactRecords = [SELECT Id, Email FROM Contact WHERE Account.Name = :accountName];
if(contactRecords.size() > 0){
for(Contact con : contactRecords){
if(con.Email != null){
emailList.add(con.Email);
}
}
}
return emailList;
}
}
🎥 Watch It in Action!
If you’re more of a visual learner, I’ve got you covered. Here’s a quick video walkthrough of how this Apex method works in Salesforce.