Sometimes you need to quickly grab the most recently created Accounts and Contacts in Salesforce maybe to display in a dashboard, audit new entries, or just for testing and debugging.
In this blog post, we’ll walk through a simple Apex method that:
Queries the 10 most recent Account records
Queries the 10 most recent Contact records
Combines them into a single list
Returns the combined result to be used elsewhere in your code
This is a great example of using SOQL, collections, and basic logic to manipulate data in Apex.
🧠 What Are We Trying to Do?
Let’s break it down:
We want to create a method that:
Grabs the top 10 Accounts (based on
CreatedDate
)Grabs the top 10 Contacts
Combines both sets into a list
Returns that list
This can help in many use cases:
Admin dashboards
Custom Visualforce or LWC components
Simple monitoring or audits
🔍 Code Explanation
public with sharing class ApexUseCaseEighteen {
public static List<Object> displayAccAndConRecords(){
List<Object> objList = new List<Object>();
We define a class called ApexUseCaseEighteen
, and create a static method called displayAccAndConRecords
which returns a list of generic Object
type because we’re combining different SObject types (Accounts and Contacts).
List<Account> accsCreated = [SELECT Id, Name FROM Account ORDER BY CreatedDate DESC LIMIT 10];
List<Contact> conCreated = [SELECT Id, Name FROM Contact ORDER BY CreatedDate DESC LIMIT 10];
We use SOQL to fetch the 10 most recently created Account and Contact records, sorted in descending order (most recent first).
if(accsCreated.size() > 0){
objList.addAll(accsCreated);
}
if(conCreated.size() > 0){
objList.addAll(conCreated);
}
We check if either list has data, and if so, we add all their entries into our final objList
.
System.debug(objList.size());
return objList;
}
}
We print the total number of records fetched (for debugging), and then return the combined list.
✅ Why This Is Useful
This code is handy for:
Showing new records in the last 24 hours
Combining results from multiple objects
Building audit tools or dashboards
Bonus Tip 💡:
If you only want records created in the last 24 hours, you can filter using SOQL date functions like:
WHERE CreatedDate = LAST_N_DAYS:1
Update your queries like so:
[SELECT Id, Name FROM Account WHERE CreatedDate = LAST_N_DAYS:1 ORDER BY CreatedDate DESC LIMIT 10];
This ensures only records from the last 24 hours are fetched!
🧾 Final Code Snippet
public with sharing class ApexUseCaseEighteen {
public static List<Object> displayAccAndConRecords(){
List<Object> objList = new List<Object>();
List<Account> accsCreated = [SELECT Id, Name FROM Account WHERE CreatedDate = LAST_N_DAYS:1 ORDER BY CreatedDate DESC LIMIT 10];
List<Contact> conCreated = [SELECT Id, Name FROM Contact WHERE CreatedDate = LAST_N_DAYS:1 ORDER BY CreatedDate DESC LIMIT 10];
if(accsCreated.size() > 0){
objList.addAll(accsCreated);
}
if(conCreated.size() > 0){
objList.addAll(conCreated);
}
System.debug(objList.size());
return objList;
}
}
🎥 Watch It in Action!
Want to see how this works in the Developer Console? Watch my short demo where I query records live and explain each step in real time.