+ Start a Discussion
Matt TindallMatt Tindall 

Illegal Assignment from LIST<aggregateResult> to Integer

I'm trying to make some code that will add up the total value of a select field within a list i have created. I then want to subtract it from a set value. But i just keep getting an error. Can anyone please advise?

public with sharing class Total{

    Private integer allowance=252;
    public integer total_used;
    
    public Total(){
        total_used = [select sum (Hours_Used__c) FROM Holiday_Request__c WHERE Staff_Memeber__r.Name = 'Dan'];
    }
    
    Public Integer gettotal_used() {
        return total_used;
    }
    
    Public Integer getAvailable() {
        return 252-21;
    }
}

 

bob_buzzardbob_buzzard

Your query will return an aggregate result, from the docs:

 

--- snip ---

 

Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results. 

 

--- snip ---

 

You need to extract the value from the first element of the result:

 

public Total(){
    List<AggregateResult> results=[select sum (Hours_Used__c) FROM Holiday_Request__c WHERE Staff_Memeber__r.Name = 'Dan'];
    total_used = results[0].get('expr0');
}

 

 

Matt TindallMatt Tindall

Thanks for your reply!

I'm now getting the error "Illegal assignment from Object to Integer"

 

public with sharing class Total{

    Private integer allowance=252;
    public integer total_used;
    
public Total(){
    List<AggregateResult> results=[select sum (Hours_Used__c) FROM Holiday_Request__c WHERE Staff_Memeber__r.Name = 'Dan'];
    total_used = results[0].get('expr0');
}
    
    Public Integer gettotal_used() {
        return total_used;
    }
    
    Public Integer getAvailable() {
        return 252-21;
    }
}

 

bob_buzzardbob_buzzard

It just needs a cast:

 

total_used = (Integer) results[0].get('expr0');

 

Matt TindallMatt Tindall

My appologies to keep asking questions.

 

I now get an error on my VF page that is arising from some error on the code, "invalid conversion from runtime type Decimal to Integer "

 

CLASS

public with sharing class Total{
    Private integer allowance=252;
    public integer total_used;
    
public Total(){
    List<AggregateResult> results=[select sum (Hours_Used__c) FROM Holiday_Request__c WHERE Staff_Memeber__r.Name = 'Dan'];
    total_used = (Integer) results[0].get('expr0');
}
    
    Public Integer getallowance() {
        return allowance;
    }
    
    Public Integer gettotal_used() {
        return total_used;
    }
    
    Public Integer getAvailable() {
        return allowance-total_used;
    }
}

 VF

<apex:page title="Dans Holidays" controller="Total">

<apex:sectionHeader subtitle="Dan" title="Holidays"/>
 
<apex:pageBlock >   

 
   <apex:pageBlockSection columns="1">
       <p>{!available}</p>      
   </apex:pageBlockSection>
 

 
</apex:pageBlock>

</apex:page>

 

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

List<AggregateResult> results=[select sum (Hours_Used__c) FROM Holiday_Request__c WHERE Staff_Memeber__r.Name = 'Dan'];
decimal temp = (decimal)results[0].get('expr0');
total_used = temp.intvalue();

bob_buzzardbob_buzzard

Don't worry about asking questions - that is what these boards are for.

 

It sounds like the field you are summing isn't an integer, rather the total is coming out as a decimal.

 

You could change your property so that it is stored as a decimal, but its probably easier to cast it as a decimal and extract the integer value - it only adds a few characters:

 

totalUser = ((decimal)results[0].get('expr0')).intValue();

 

Ariel GorfinkelAriel Gorfinkel
Similar issue to https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Aa8FIAS
Convert to string as a mediate, try:
total_used = Integer.valueOf(results[0].get('expr0') + '');
Souvik Kundu 3Souvik Kundu 3
Ariel Gorfinkel - Mistakenly disliked it. Your answer is perfect.
Kellan ScheiberKellan Scheiber
bob_buzzard, i just used your solution for a similar issue and it worked great!
Preetika Gupta 8Preetika Gupta 8
Illegal assignment from List<Lead> to Integer

I am trying to do this can someone please help me 

public static List<rComWrapper> getRComList(Id LeadId) {
      Integer i = [SELECT Id, Rooms__c FROM Lead where Id =:LeadId];
      List<LeadRecommedation__c> lrCom = [SELECT Id, Name, Lead__c, Recommendation__c, Selection__c FROM LeadRecommedation__c where Lead__c =:LeadId];
      List<Recommendation> recommendationList = [SELECT Id, Name,Description from Recommendation LIMIT: i];