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
Stephen E 4Stephen E 4 

List has more than 1 row for assignment to SObject... but we need to display more than one row.

I am trying to do a summary a a custom time object we have for Cases. I have embded the VFP on a cases page layout. When people enter time, it groups together the person entering the time, and the amount they have billed. But I keep getting the 'List has more than 1 row for assignment to SObject' error. I am not sure why, this query will 99% percent of the time have more than 1 record (or more than 1 person entering records) because of how our cases work. Why cant I display more than 1 record? Other Programs we have made will return multipe records and then we loop through them on the VFP to display. Not sure why there is a limit on this program though. 

Code:
 
public class TimeSummaryController {
    public string caseid {get;set;}
     private final Case cc;
    public AggregateResult timeq {get;set;}
    public TimeSummaryController(ApexPages.StandardController stdController){
        this.cc = (Case)stdController.getRecord();
        
    }
    
    public AggregateResult getTimes(){
        try {
       	 timeq = [SELECT Engineer_Name__r.Name enr,SUM(Total_Billed__c)total_billed FROM Time__c WHERE Case__c = :cc.Id GROUP BY Engineer_Name__r.Name ORDER BY SUM(Total_Billed__c) DESC];
        }catch(Exception e){
            System.debug(e);
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No Time To Display'));
    		return null;        
        }
        return timeq;
    }
}
 
<apex:page standardController="Case" extensions="TimeSummaryController" showHeader="false">
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlock>
   <apex:pageBlockTable value="{!Times}" var="tt" id="rr">
		
        <apex:column value="{!tt['enr']}"/>
       <apex:column value="{!tt['total_billed']}"/>

    </apex:pageBlockTable>
        </apex:pageBlock>
</apex:page>

 
Best Answer chosen by Stephen E 4
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
public class TimeSummaryController 
{
    public string caseid {get;set;}
    private final Case cc;
    public List<AggregateResult> timeq {get;set;}
    public TimeSummaryController(ApexPages.StandardController stdController){
        this.cc = (Case)stdController.getRecord();
        
    }
    
    public List<AggregateResult> getTimes(){
        try {
			timeq = [SELECT Engineer_Name__r.Name enr,SUM(Total_Billed__c)total_billed FROM Time__c WHERE Case__c = :cc.Id GROUP BY Engineer_Name__r.Name ORDER BY SUM(Total_Billed__c) DESC];
        }catch(Exception e){
            System.debug(e);
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No Time To Display'));
    		return null;        
        }
        return timeq;
    }
}

Let us know if this will help you
 

All Answers

Paul S.Paul S.
Your AggregateResult query is returning a list of items, but you've declared your AggregateResult variable as a single AggregateResult sObject.  Essentially, you're trying to add multiple items to something that will only hold one.  You'll need to make the AggregateResult variable a list and then loop through that list to display the results on your Vf page.
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
public class TimeSummaryController 
{
    public string caseid {get;set;}
    private final Case cc;
    public List<AggregateResult> timeq {get;set;}
    public TimeSummaryController(ApexPages.StandardController stdController){
        this.cc = (Case)stdController.getRecord();
        
    }
    
    public List<AggregateResult> getTimes(){
        try {
			timeq = [SELECT Engineer_Name__r.Name enr,SUM(Total_Billed__c)total_billed FROM Time__c WHERE Case__c = :cc.Id GROUP BY Engineer_Name__r.Name ORDER BY SUM(Total_Billed__c) DESC];
        }catch(Exception e){
            System.debug(e);
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No Time To Display'));
    		return null;        
        }
        return timeq;
    }
}

Let us know if this will help you
 
This was selected as the best answer
Stephen E 4Stephen E 4
Thank you both! Making the AggregateResult a list works.