-1

I have a requirement to get a table as below, enter image description here

I was creating an apex controller as below, but its showing issues, May i know where i get wrong ?

public class AccountController {
    public <Account> getAccountsWithOwners() {
        List<Account> accountsWithOwners = [SELECT Count(Id), Owner.Name, (SELECT(count(Id) FROM Opportunities) FROM Account GROUP BY Owner.Name];
        return accountsWithOwners;                                                              
    }
}
1
  • 1
    Hi Sourav, welcome to SFSE. Please take a moment to scroll through the tour and read How to Ask. As written, this post is not very specific and will not likely receive a useful answer. Please edit it to be more detailed than "showing issues".
    – Adrian Larson
    May 11 at 13:03

1 Answer 1

1

The main issue with the query is that aggregate queries are supported only at root level. Inner query for counting opportunities will not work in this case.

We can execute 2 queries for getting the desired results as below :

1 Query Accounts based on Owner.Name as below :

AggregateResult[] accountResults = [SELECT Count(Id),Owner.Name FROM Account GROUP BY Owner.Name ORDER BY Owner.Name]; 

2 Query Opportunities based on the related account owner field as below :

AggregateResult[] opportunityResults = [SELECT count(id), Account.Owner.Name FROM Opportunity GROUP BY Account.Owner.Name ORDER BY Account.Owner.Name];   

In both the queries we are using the same Owner.name field for aggregating as well as ordering which enables us to conclude that 1st result of query 2 will be the count of opportunities for 1st result of query 1 and same for other results as well.

Edit : Either have separate methods or for returning both results from the method, Using a map to store both query results.

public Map<String, List<AggregateResult>> getAccountsAndOppsWithOwners() {

Map<String, List<AggregateResult>> aggResultsMap = new Map<String, List<AggregateResult>>();

AggregateResult[] accountResults = [SELECT Count(Id),Owner.Name FROM Account GROUP BY Owner.Name ORDER BY Owner.Name]; 
AggregateResult[] opportunityResults = [SELECT count(id), Account.Owner.Name FROM Opportunity GROUP BY Account.Owner.Name ORDER BY Account.Owner.Name];   

aggResultsMap.put('accounts', accountResults);
aggResultsMap.put('opportunities', opportunityResults);

return aggResultsMap;
}
5
  • If i am using the below, its showing me an "unreachable statement" error. whats can be the issue? public class AccountController { public List <AggregateResult>getAccountsWithOwners(){ List <AggregateResult> accountResults = [SELECT Count(Id),Owner.Name FROM Account GROUP BY Owner.Name ORDER BY Owner.Name]; List <AggregateResult> opportunityResults = [SELECT count(id), Account.Owner.Name FROM Opportunity GROUP BY Account.Owner.Name ORDER BY Account.Owner.Name]; return accountResults; return opportunityResults; } }
    – Sourav
    May 12 at 7:29
  • while using only the account is working
    – Sourav
    May 12 at 7:40
  • Making two separate methods working. Can't we make both query under one method ?
    – Sourav
    May 12 at 7:50
  • You cannot return from a function twice. Updated the answer for using only 1 method. My preference would be to use separate methods.
    – Rohit
    May 12 at 8:05
  • 1
    Thanks Rohit. It worked
    – Sourav
    May 12 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .