You need to sign in to do that
Don't have an account?
Matt 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; } }
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:
Thanks for your reply!
I'm now getting the error "Illegal assignment from Object to Integer"
It just needs a cast:
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
VF
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();
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:
Convert to string as a mediate, try:
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];