Just want to know what am I missing here.

trigger updateLastCallDate on Task (after insert, after update, after delete) {

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

    List<Contact> con_list = new List<Contact>();

    for(Task T: Trigger.new()){
        if(String.valueof(T.WhatId).startsWith('001') &&
            T.Status=='Completed' && T.Subject=='Call' ) {

     for (AggregateResult aggregateResult: [SELECT max(createdDate)MaxCDate, WhatId
        FROM Task WHERE WhatID IN: con_set AND Status ='Completed'
        AND Subject ='Call' Group By WhatId]) {
        con_list.add(new Contact(Id=(id)aggregateResult.get('WhatId'),Last_Call__c=date.valueof(aggregateResult.get('MaxCDate'))));

     try {
         if(con_list !=null && con_list.size()>0) {
             update con_list;
     } catch(Exception e) {
         system.debug('Exception ***'+e.getMessage());
  • Can you provide more information? Are you receiving an error? If so, can you include it in your question? – Nick Cook Dec 18 '15 at 2:44
  • one mistake i noticed you are adding AccountId in con_set this set and below assigning AccountId to contact Contact(Id=(id)aggregateResult.get('WhatId').... Except this code look good to me. pls specify line no where you are getting eror – Ratan Paul Dec 18 '15 at 2:47

The compile error is because you have this:

for(Task T: Trigger.new()){

so the compiler thinks .new() is a method on a variable named Trigger, when it's actually a system context variable. It should be:

for (Task t : trigger.new) {
  • 2
    Nice one. It's only now that you've posted this that the question makes sense. – Nick Cook Dec 18 '15 at 3:25
  • ohh yes this is the mistake. – Ratan Paul Dec 18 '15 at 4:40
  • Thansk for the comments guys but... It's giving me an error... Trigger name exists on different SObject type: Contact If I change Task to contact then I have to change all "Task" then it gives me an error that WHATID doesnt exist. Isnt the sObject has to be on contact? – Ninja King Dec 18 '15 at 17:35
  • That error means that you already have a trigger on Contact named the same thing...rename this one to resolve it. If this answer answered your question and if you have a different question, please create a new post :) – Brian Mansfield Dec 18 '15 at 17:37
  • I actually dont. :( I renamed a couple of times and the same error – Ninja King Dec 18 '15 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.