+ Start a Discussion
Chad MoutesChad Moutes 

Need Help with Apex Code Error

I am creating a trigger that display the Last Completed Call date on the Account. I have the following code
 
trigger LastCompletedCallDate on Task (after insert, after update, after delete) {

    Set<Id> acc_set = new Set<Id>();    

    List<Account> acc_list = new List<Account>();
     
    if(trigger.isInsert || trigger.isUpdate){
    for(Task T: Trigger.new){
        if(String.valueof(T.WhatId).startsWith('001') && T.Status=='Completed' && T.Subject=='Completed Call' ){
            acc_set.add(T.WhatId);
        }
         
     }
     }
     if(trigger.isDelete){
        for(Task T: Trigger.old){
        if(String.valueof(T.WhatId).startsWith('001') && T.Status=='Completed' && T.Subject=='Completed Call' ){
            acc_set.add(T.WhatId);
        }
         
     }
     }
      
     for(AggregateResult aggregateResult:[SELECT max(createdDate)MaxCDate,WhatId FROM Task WHERE WhatID IN: acc_set AND Status ='Completed' AND Subject ='Completed Call' Group By WhatId]){
         acc_list.add(new Account(Id=(id)aggregateResult.get('WhatId'),Last_Completed_Call__c=aggregateResult.get('MaxCDate')));
          
     }
      
     try{
      
         if(acc_list !=null && acc_list.size()>0){
             update acc_list;
         }
      
     }Catch(Exception e){
         system.debug('Exception ***'+e.getMessage());
       
      }
 
}

And I'm receiving this error: Compile Error: Invalid field initializer: Date.Last_Completed_Call__c at line 25 column 72
David ZhuDavid Zhu
aggregateResult.get('MaxCDate') return object type.
You may use
   datetime dt = datetime.parse(string.valueof(aggregateResult.get('MaxCDate')));
and assign dt to last_completed_call__c.