+ Start a Discussion
Niraj Kumar 9Niraj Kumar 9 

trigger on task to count the activity history and uodate the contact field Count of Activity

 Trigger saves but not updating the contact field, Its Urgent,  HELP ME.....
Thanks In advance....
 

Here is my trigger :


trigger ContactCountActivity on task (after insert, after update) {

 
  map<id,integer> mcontactCount = new map<id,integer>();
  integer Count_of_Activity = 0;


if(Trigger.isinsert){
  for(task t :trigger.new) {
    if(t.whatId != null) {
      if(string.valueOf(t.whatId).left(3) == '006') {
         
         
         if(mcontactCount.containsKey(t.whatId)) {
           Count_of_Activity = mcontactCount.get(t.whatId);
         }
         Count_of_Activity = Count_of_Activity + 1;
         mcontactCount.put(t.whatid,Count_of_Activity);
      }
    }
  }
}
   

if(Trigger.isdelete){
  for(task t1 :trigger.old) {
    if(t1.whatId != null) {
     if(string.valueOf(t1.whatId).left(3) == '006') {
         
         
         if(mcontactCount.containsKey(t1.whatId)) {
           Count_of_Activity = mcontactCount.get(t1.whatId);
         }
         Count_of_Activity = Count_of_Activity - 1;
         mcontactCount.put(t1.whatid,Count_of_Activity);
      }
    }
  }
}
  if(mcontactCount.keyset().size()>0) {
    list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
    for(contact o :contactToUpdate) {
      o.Count_of_Activity__c = mcontactCount.get(o.id);
    }
    if(contactToUpdate.size()>0) {
      update contactToUpdate;
    }
  }
 }
Best Answer chosen by Niraj Kumar 9
DebasisDebasis
Hi Niraj,

can you please add some debug message and post here. Set debug log and post the log here for below code
 
trigger ContactCountActivity on task (after insert, after update,after delete) {

 
  map<id,integer> mcontactCount = new map<id,integer>();
  integer Count_of_Activity = 0;


if(Trigger.isinsert){
system.debug('***trigger.new');
  for(task t :trigger.new) {
system.debug('***task is'+t);
system.debug('***task who id'+t.whoid);
system.debug('***mcontactCount-1'+mcontactCount);

    if(t.whoId != null) {
      if(string.valueOf(t.WhoId).left(3) == '003') {
         
         
         if(mcontactCount.containsKey(t.WhoId)) {
           Count_of_Activity = mcontactCount.get(t.WhoId);
         }
		 
         Count_of_Activity = Count_of_Activity + 1;
         mcontactCount.put(t.whoid,Count_of_Activity);
      }
    }
  }
}
  system.debug('*** after insert mcontactCount'+mcontactCount); 

if(Trigger.isdelete){
  for(task t1 :trigger.old) {
    if(t1.WhoId != null) {
     if(string.valueOf(t1.WhoId).left(3) == '003') {
         
         
         if(mcontactCount.containsKey(t1.WhoId)) {
           Count_of_Activity = mcontactCount.get(t1.WhoId);
         }
         Count_of_Activity = Count_of_Activity - 1;
         mcontactCount.put(t1.WhoId,Count_of_Activity);
      }
    }
  }
}
 system.debug('*** after delete mcontactCount'+mcontactCount); 

  if(mcontactCount.keyset().size()>0) {
    list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
system.debug('**contactToUpdate '+contactToUpdate );
    for(contact o :contactToUpdate) {
system.debug('**activity for the contact'+mcontactCount.get(o.id));
      o.Count_of_Activity__c = mcontactCount.get(o.id);
    }
    if(contactToUpdate.size()>0) {
      update contactToUpdate;
    }
  }
 }

please give me this log message where i have added debug.log line.

 

All Answers

DebasisDebasis
Hi Niraj,
In this trigger you have used trigger.isDelete context varibale but you are not executing this trigger in delete event.

first make the change in first line as :
     trigger ContactCountActivity on task (after insert, after update,after delete) {

secondly, here 006 is a key prefix for Opportunity recrds, as you are working on contact records, please use 003 as key prefix in comparision.
change this line  if(string.valueOf(t.whatId).left(3) == '006') { as below
 if(string.valueOf(t.whatId).left(3) == '003') {

please check it and let me know if it solves your issue.

Thanks,
Debasis
Niraj Kumar 9Niraj Kumar 9
Hi Debasis,
Thanks for reply.
I made changes in code, but still not updating the field Count_of_Activity__c  (Number data type) in contact.
DebasisDebasis
Hi Niraj,

please use whoId instead of WhatId, because 
                 WhoID refers to people things.  So that would be typically a Lead ID or a Contact ID
                  WhatID refers to object type things.  That would typically be an Account ID or an Opportunity ID
i have modified your code as below.
trigger ContactCountActivity on task (after insert, after update,after delete) { 
  map<id,integer> mcontactCount = new map<id,integer>();
  integer Count_of_Activity = 0;
if(Trigger.isinsert){
  for(task t :trigger.new) {
    if(t.whoId != null) {
      if(string.valueOf(t.WhoId).left(3) == '003') {        
         
         if(mcontactCount.containsKey(t.WhoId)) {
           Count_of_Activity = mcontactCount.get(t.WhoId);
         }         
         Count_of_Activity = Count_of_Activity + 1;
         mcontactCount.put(t.whoid,Count_of_Activity);
      }
    }
  }
}  

if(Trigger.isdelete){
  for(task t1 :trigger.old) {
    if(t1.WhoId != null) {
     if(string.valueOf(t1.WhoId).left(3) == '003') {        
         
         if(mcontactCount.containsKey(t1.WhoId)) {
           Count_of_Activity = mcontactCount.get(t1.WhoId);
         }
         Count_of_Activity = Count_of_Activity - 1;
         mcontactCount.put(t1.WhoId,Count_of_Activity);
      }
    }
  }
}
  if(mcontactCount.keyset().size()>0) {
    list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
    for(contact o :contactToUpdate) {
      o.Count_of_Activity__c = mcontactCount.get(o.id);
    }
    if(contactToUpdate.size()>0) {
      update contactToUpdate;
    }
  }
 }
please let me know it helps you.
Niraj Kumar 9Niraj Kumar 9
Hi Debasis,
Still not updating.
DebasisDebasis
Hi Niraj,

can you please add some debug message and post here. Set debug log and post the log here for below code
 
trigger ContactCountActivity on task (after insert, after update,after delete) {

 
  map<id,integer> mcontactCount = new map<id,integer>();
  integer Count_of_Activity = 0;


if(Trigger.isinsert){
system.debug('***trigger.new');
  for(task t :trigger.new) {
system.debug('***task is'+t);
system.debug('***task who id'+t.whoid);
system.debug('***mcontactCount-1'+mcontactCount);

    if(t.whoId != null) {
      if(string.valueOf(t.WhoId).left(3) == '003') {
         
         
         if(mcontactCount.containsKey(t.WhoId)) {
           Count_of_Activity = mcontactCount.get(t.WhoId);
         }
		 
         Count_of_Activity = Count_of_Activity + 1;
         mcontactCount.put(t.whoid,Count_of_Activity);
      }
    }
  }
}
  system.debug('*** after insert mcontactCount'+mcontactCount); 

if(Trigger.isdelete){
  for(task t1 :trigger.old) {
    if(t1.WhoId != null) {
     if(string.valueOf(t1.WhoId).left(3) == '003') {
         
         
         if(mcontactCount.containsKey(t1.WhoId)) {
           Count_of_Activity = mcontactCount.get(t1.WhoId);
         }
         Count_of_Activity = Count_of_Activity - 1;
         mcontactCount.put(t1.WhoId,Count_of_Activity);
      }
    }
  }
}
 system.debug('*** after delete mcontactCount'+mcontactCount); 

  if(mcontactCount.keyset().size()>0) {
    list<contact> contactToUpdate = new list<contact>([SELECT id,Count_of_Activity__c FROM contact WHERE Id IN :mcontactCount.keyset()]);
system.debug('**contactToUpdate '+contactToUpdate );
    for(contact o :contactToUpdate) {
system.debug('**activity for the contact'+mcontactCount.get(o.id));
      o.Count_of_Activity__c = mcontactCount.get(o.id);
    }
    if(contactToUpdate.size()>0) {
      update contactToUpdate;
    }
  }
 }

please give me this log message where i have added debug.log line.

 
This was selected as the best answer
Niraj Kumar 9Niraj Kumar 9
Hi debasis,
Thanks for the help.
 After  two changes, Its work.
1) Assign County of activity (on contact object) field value to 0.

2) o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);

here is the trigger. can u please provide me the test classes.
 
trigger opportunitycountevent on Event (after insert, after delete) {
    map<id,integer> mopportunityCount = new map<id,integer>();
         integer Countevent =0;
  if (Trigger.isafter){
       if(Trigger.isinsert){

  for(event e :trigger.new) {


    if(e.whatId != null) {
      if(string.valueOf(e.WhatId).left(3) == '006') {
         
         
         if(mopportunityCount.containsKey(e.WhatId)) {
           Countevent  = mopportunityCount.get(e.WhatId);
            }
         
         Countevent  = Countevent   + 1;
      
         mopportunityCount.put(e.whatid,Countevent);
      }
    }
  }
}
 

if(Trigger.isdelete){
  for(event e1 :trigger.old) {
    if(e1.WhatId != null) {
     if(string.valueOf(e1.WhatId).left(3) == '006') {
         
         
         if(mopportunityCount.containsKey(e1.WhatId)) {
           Countevent   = mopportunityCount.get(e1.WhatId);
         }
         Countevent = Countevent - 1;
         mopportunityCount.put(e1.WhatId,Countevent);
      }
    }
  }
}


  if(mopportunityCount.keyset().size()>0) {
    list<opportunity> opportunityToUpdate = new list<opportunity>([SELECT id,Count_of_Activity__c FROM opportunity WHERE Id IN :mopportunityCount.keyset()]);
     for(opportunity o :opportunityToUpdate ) {
     o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);
    }
   if(opportunityToUpdate.size()>0) {
      update opportunityToUpdate;
    }
   }
  }
  }

Here is the code its working.
trigger opportunitycountevent on Event (after insert, after delete) {
    map<id,integer> mopportunityCount = new map<id,integer>();
         integer Countevent =0;
  if (Trigger.isafter){
       if(Trigger.isinsert){

  for(event e :trigger.new) {


    if(e.whatId != null) {
      if(string.valueOf(e.WhatId).left(3) == '006') {
         
         
         if(mopportunityCount.containsKey(e.WhatId)) {
           Countevent  = mopportunityCount.get(e.WhatId);
            }
         
         Countevent  = Countevent   + 1;
      
         mopportunityCount.put(e.whatid,Countevent);
      }
    }
  }
}
 

if(Trigger.isdelete){
  for(event e1 :trigger.old) {
    if(e1.WhatId != null) {
     if(string.valueOf(e1.WhatId).left(3) == '006') {
         
         
         if(mopportunityCount.containsKey(e1.WhatId)) {
           Countevent   = mopportunityCount.get(e1.WhatId);
         }
         Countevent = Countevent - 1;
         mopportunityCount.put(e1.WhatId,Countevent);
      }
    }
  }
}


  if(mopportunityCount.keyset().size()>0) {
    list<opportunity> opportunityToUpdate = new list<opportunity>([SELECT id,Count_of_Activity__c FROM opportunity WHERE Id IN :mopportunityCount.keyset()]);
     for(opportunity o :opportunityToUpdate ) {
     o.Count_of_Activity__c = o.Count_of_Activity__c + mopportunityCount.get(o.id);
    }
   if(opportunityToUpdate.size()>0) {
      update opportunityToUpdate;
    }
   }
  }
  }

Thanks for the help.

regards,
Niraj Kumar