function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nr123nr123 

AggregateResults doesnt recognize Owner Name Field

I need to display a summary of Opportunities by Opportunity Owner in a Visualforce Page Dashboard component. So far I can get it to group by OwnerId, but it doesnt work for Owner.Name. I get the following error: "System.SObjectException: Invalid field Owner.Name for AggregateResult".

 

 

 

public class QuotesRecvd{

    Public String quotecount {get; set;}
    Public String OName {get;set;}
    
    public QuotesRecvd(string n ,string q){     
        this.oName = n;
        this.quotecount = q;
        
    }
}

       public List<QuotesRecvd> QuotesRecvdList = new List<QuotesRecvd>();    
           public List<QuotesRecvd> getQuotesRecvdCount(){
       
              AggregateResult[] agr = [SELECT Owner.Name, count(Name)FROM Opportunity WHERE Quote_Requested__c = :date.today()-1 Group By rollup (Owner.Name)];
           for(AggregateResult qrcvd : agr){
               QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Owner.Name')),String.valueof(qrcvd.get('expr0'))));
               }
               return QuotesRecvdList;
        
}

 

Any suggestions? I also need to know how to create a test for this.

 

 

Any help would be greatly appreciated!

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Change this

 

 

QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Owner.Name')),String.valueof(qrcvd.get('expr0'))));

 

 

to

 

QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Name')),String.valueof(qrcvd.get('expr0'))));

 

This will give you owner name

 

The reason for this is that whenever you use native Name field of any object for group by aggr query return Name field. Like in above example you used Owner.Name that is Name field of User object so it return Name field in aggr result.

All Answers

Shashikant SharmaShashikant Sharma

Change this

 

 

QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Owner.Name')),String.valueof(qrcvd.get('expr0'))));

 

 

to

 

QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Name')),String.valueof(qrcvd.get('expr0'))));

 

This will give you owner name

 

The reason for this is that whenever you use native Name field of any object for group by aggr query return Name field. Like in above example you used Owner.Name that is Name field of User object so it return Name field in aggr result.

This was selected as the best answer
nr123nr123

That fixed it!  Thank you.

Shashikant SharmaShashikant Sharma

Your Welcome :)

stratusstratus

hi Shashikant 

 

Our of query is select name,Qwner.Name from account 

then i think your soltion

 

QuotesRecvdList.add(new QuotesRecvd(String.valueof(

qrcvd.get('Name')

),String.valueof(qrcvd.get('expr0'))));

 will not work