In the world of Salesforce, Opportunities represent potential revenue and Accounts are the businesses behind them. Sometimes, you want to identify all the unique accounts that are tied to high-value opportunities, such as those with revenue greater than 80,000.
In this blog post, we’ll write a simple Apex method that:
Pulls all Opportunity records with an Amount greater than 80K
Extracts the associated Account IDs
Returns a unique list of these Account IDs using a
Set
This method can be helpful for account managers, sales leads, or dashboards where we want to see which companies are really driving business growth.
🧠 What Are We Trying to Do?
Let’s say your company wants to focus on high-paying clients those whose deals are consistently over $80,000. You need a report that answers: “Which accounts have at least one opportunity over 80K?”
Our method will:
Query Opportunity records where
Amount > 80000
Pull each related Account ID from those opportunities
Store the IDs in a Set, which automatically ensures there are no duplicates
Return the set of unique Account IDs
🔍 Code Explanation
public with sharing class ApexUseCaseTwentySeven {
public static Set<Id> generateAccountReport(){
We create a class called ApexUseCaseTwentySeven
, and inside it, define a public static method named generateAccountReport
that returns a Set<Id>
— this will hold our Account IDs.
Set<Id> accountList = new Set<Id>();
We initialize an empty set to store our results. Sets are great because they automatically prevent duplicates.
List<Opportunity> oppWithGoodRevenue = [
SELECT Id, Name, Amount, AccountId
FROM Opportunity
WHERE Amount > 80000
];
This SOQL query retrieves all Opportunity
records where the Amount
is greater than 80K. We also pull the associated AccountId
.
for(Opportunity opp : oppWithGoodRevenue){
accountList.add(opp.AccountId);
}
We loop through each opportunity and add its AccountId
to the set. If an account has multiple high-value opportunities, its ID will appear only once in the set.
return accountList;
}
}
Finally, we return the set of unique Account IDs.
✅ Why This Is Useful
This Apex method is super handy when you need to:
Generate targeted reports for high-revenue clients
Create a campaign for account execs to follow up
Track success rates of premium-tier clients
Want to take this further? You could:
Return a list of
Account
names instead of just IDsAdd filters like Opportunity
StageName = 'Closed Won'
Integrate this into a scheduled batch process
🧾 Final Code Snippet
public with sharing class ApexUseCaseTwentySeven {
public static Set<Id> generateAccountReport(){
Set<Id> accountList = new Set<Id>();
List<Opportunity> oppWithGoodRevenue = [SELECT Id, Name, Amount, AccountId FROM Opportunity WHERE Amount > 80000];
for(Opportunity opp : oppWithGoodRevenue){
accountList.add(opp.AccountId);
}
return accountList;
}
}
🎥 Watch It in Action!
Want to see how it works live in Developer Console or a Lightning Component? Watch this short demo video where I explain each step and show how the unique account list is generated from Opportunities.