+ Start a Discussion

Illegal assignment from LIST<AggregateResult> to Date

Error: Compile Error: Illegal assignment from LIST<AggregateResult> to Date at line 9 column 7


trigger update_expiry_date on opportunity (after insert, after update) {
if (trigger.isUpdate) {
  List<Opportunity> opptemp = new List<Opportunity>();
  for(Opportunity o : trigger.new){
    String oppid = o.id;
    if ([SELECT COUNT(license_term_end__c) FROM opportunitylineitem WHERE opportunityid = :oppid] > 0) {
      String accid = o.accountid;
      Account updateacc = [SELECT id, expiry_date_2__c FROM account WHERE id = :accid];
      updateacc.expiry_date_2__c = [SELECT MAX(license_term_end__c) FROM opportunitylineitem WHERE opportunityid =:oppid];

 Both of the fields expiry_date_2__c and license_term_end__c are date fields. Does the Max command just not work on this. That could be it I suppose. Please help thanks. I'm pretty much just trying to do a max roll-up and have it write the value to the date field expiry_date_2__c.




Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects and you can't assign it to a date directly.This is the reason why you are getting error " Illegal assignment from LIST<AggregateResult> to Date".


You should try in this way.


 List<AggregateResult> lst= [SELECT MAX(license_term_end__c)maxvalue FROM opportunitylineitem WHERE opportunityid =:oppid];

for (AggregateResult ar : lst)
        updateacc.expiry_date_2__c=Date.Valueof(ar.get('maxvalue '));


For more help see Apex guide.

Patti AbeyratnePatti Abeyratne
List<AggregateResult> datelist = [SELECT max(Date_Time__c)FROM Booking__c WHERE Contact__C = :objContact.ID ];
          dateMostRecentBooking   =Date.valueof(datelist.get())  ;

this assignment didn't work, anybody knows the answer?
M RahmanM Rahman
Hope you've got it at the end. Following code should work - 

List<AggregateResult> datelist = [SELECT max(Date_Time__c)maxDateTime FROM Booking__c WHERE Contact__C = :objContact.ID ];
          dateMostRecentBooking   =Date.valueof(datelist[0].get('maxDateTime'));

Modifications are in italic bold