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
Sainath VenkatSainath Venkat 

apex class error for Illegal assignment from List<AggregateResult> to List<Time__c>

I am working on apex class and visualforce page where I want to group records by Name and sum of hours but I am getting the below error.
"Illegal assignment from List<AggregateResult> to List<Time__c>"
Can anyone help me out in this issue,My code is below
public class RolesFetch {
        public list<Time__c> projectrole { get; set; }
    
    public datetime startdate1;
    public datetime enddate1;
   public Time__c a { get; set; } 
    public RolesFetch(){
     projectrole=new list< Time__c>();
    a=new Time__c();
     
    }
    public PageReference Fetch() {
    startdate1=a.From_Date__c;
    enddate1=a.To_Date__c;
    projectrole=[select Role__c,SUM(Hourly_Rate__c),Date__c,SUM(Hours__c),SUM(Billable_Dollars__c) from Time__c GROUP BY Role__c,Date__c HAVING Date__c>=:startdate1.date() AND Date__c<=:enddate1.date() ];
    return null;
    }

}
My VF Page
<apex:page controller="RolesFetch">
 <apex:form id="dt1">
 <apex:pageBlock >
 <apex:pageBlockSection >
 <apex:pageBlockSectionItem >
 <apex:outputLabel >Start Date</apex:outputLabel>
 <apex:inputfield value="{!a.From_Date__c}"/>
</apex:pageBlockSectionItem>
 <apex:pageBlockSectionItem >
 <apex:outputLabel >End Date</apex:outputLabel>
 <apex:inputField value="{!a.To_Date__c}"/>
 </apex:pageBlockSectionItem>
  
<apex:commandButton value="Fetch" action="{!Fetch}" reRender="dt"/>
 
</apex:pageBlockSection>
 <apex:pageBlockTable value="{!projectrole}" var="pr" id="dt">
<apex:column headerValue="Role Name">
<apex:outputText value="{!pr.Role__c}" />
</apex:column>
<apex:column headerValue="Rate">
  <apex:outputText value="{!pr.Hourly_Rate__c}"/>
  </apex:column>
     <apex:column headerValue="Hours">
     <apex:outputText value="{!pr.Hours__c}"/>
     </apex:column>
     <apex:column headerValue="Sum">
  <apex:outputText value="{!pr.Billable_Dollars__c}"/>
  </apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
 </apex:form>
</apex:page>


 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sainath,

Greetings to you!

You are collecting the aggregate results from the query to list of type Time__c. Please change it to AggregateResult type.

Please refer to the below links which might help you further with the above requirement.

http://sfdc.arrowpointe.com/2010/02/10/using-aggregate-functions/

http://www.infallibletechie.com/2013/07/aggregateresult-in-salesforce.html

https://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Sainath VenkatSainath Venkat
Hello Khan Anas,
Hope you ding great, I did not get where to chnage things in my code, can you please help me out in this issue if possible.
Priyananth RPriyananth R
Hi Sai,

Above you have used AggregateResult. you are unable to assign Sobject list.
Just try below code, I hope it will helpful

Visualforce Page :
<apex:page controller="RolesFetch">
 <apex:form id="dt1">
 <apex:pageBlock >
 <apex:pageBlockSection >
 <apex:pageBlockSectionItem >
 <apex:outputLabel >Start Date</apex:outputLabel>
 <apex:inputfield value="{!a.From_Date__c}"/>
</apex:pageBlockSectionItem>
 <apex:pageBlockSectionItem >
 <apex:outputLabel >End Date</apex:outputLabel>
 <apex:inputField value="{!a.To_Date__c}"/>
 </apex:pageBlockSectionItem>
  
<apex:commandButton value="Fetch" action="{!Fetch}" reRender="dt"/>
 
</apex:pageBlockSection>
 <apex:pageBlockTable value="{!projectrole}" var="pr" id="dt">
<apex:column headerValue="Role Name">
<apex:outputText value="{!pr['role']}" />
</apex:column>
<apex:column headerValue="Rate">
  <apex:outputText value="{!pr['HourRate']}"/>
  </apex:column>
     <apex:column headerValue="Hours">
     <apex:outputText value="{!pr['hour']}"/>
     </apex:column>
     <apex:column headerValue="Sum">
  <apex:outputText value="{!pr['billDollar']}"/>
  </apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
 </apex:form>
</apex:page>

Apex Class :
public class RolesFetch {
    public list<AggregateResult> projectrole { get; set; }
    public datetime startdate1;
    public datetime enddate1;
    public Time__c a { get; set; } 
    public RolesFetch(){
     projectrole=new list<AggregateResult>();
    a=new Time__c();
     
    }
    public PageReference Fetch() {
    startdate1=a.From_Date__c;
    enddate1=a.To_Date__c;
    projectrole=[select Role__c role,SUM(Hourly_Rate__c) HourRate,Date__c Date,SUM(Hours__c) hour,SUM(Billable_Dollars__c) billDollar from Time__c GROUP BY Role__c,Date__c HAVING Date__c>=:startdate1.date() AND Date__c<=:enddate1.date() ];
    return null;
    }
}

 
Sainath VenkatSainath Venkat
Hi Priyananth R.
Hope you doing good.
I tried with your code but its giving me the below error when I click on fetch button.

field 'Role__c' can not be grouped in a query call
Error is in expression '{!Fetch}' in component <apex:commandButton> in page testrole123: Class.testfetch.Fetch: line 14, column 1
An unexpected error has occurred. Your development organization has been notified.


What actually I ma trying to r my requirement is below.
 I have an object called "Time__c" Time__c has fields like "Role__c" , "Rate__c" "Hours__c", now my requirement is that I need to fetch all the records of time and show on VF Page but if Role__c is same and rate__c is same then I want those records to combine and have total of hours.
For example,
Record1 has Role__c= Developer        Rate__c=8       and      Hours__c=8
Record2 has Role__c=Admin              Rate__c=6       and      Hours__C=5
Record3 has Role__c=Developer         Rate__c=8       and      Hours__c=7, then I should get only one record on VF Page for Developer role which should give 8+7=15hrs.

If Role names are same and are having different rates then I should show two developer roles on vf page Can you please help me out how to achieve this.
Priyananth RPriyananth R
Hi,

According to your requirement, I have provided one sample code. 
just convert this coding to your conditions and  try it.

Visualforce Page :
<apex:page controller="RolesFetch">
    <apex:form id="dt1">
        <apex:pageBlock >
            <apex:pageBlockTable value="{!projectrole}" var="pr" id="dt">
                <apex:column headerValue="Role Name">
                    <apex:outputText value="{!pr['role']}" />
                </apex:column>
                <apex:column headerValue="Rate">
                    <apex:outputText value="{!pr['rate']}"/>
                </apex:column>
                <apex:column headerValue="Hours">
                    <apex:outputText value="{!pr['hour']}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class :
public class RolesFetch {
    public list<AggregateResult> projectrole { get; set; }
    public RolesFetch(){
        projectrole=new list<AggregateResult>();
        projectrole=[SELECT Role__c role, SUM(Rate__c) rate, SUM(Hours__c) hour FROM Time__c GROUP BY Role__c];
    }
}
Output :

User-added image
 
Sainath VenkatSainath Venkat
Hi Priyananth R,

Thanks a lot for helping out,the code which you have provided worked but the thing is that I need to show one record on VF Page if role is same and rate is same, if role is same and rate is different then I want to show as different records on vf page.

Record1 has Role__c= Developer        Rate__c=8       and      Hours__c=8
Record2 has Role__c=Admin               Rate__c=6       and      Hours__C=5
Record3 has Role__c=Developer         Rate__c=8       and      Hours__c=7
Record4 has Role__c=Developer         Rate__c=5       and      Hours__c=5

On my vf page since record1 and 3 has has same role and same rate, I can group those 2 and show as single record but record4 is having same role as record1 and 3 but rate is different, so record4 should be independent on vf page.