+ Start a Discussion
Bayarea 101Bayarea 101 

Lead Activity Counter

here is the code for lead activity counter
trigger UpdateLeadCounts on Task (after delete, after insert) {

set<ID> LeadIds= new Set<ID>();
for(Task tt:trigger.new)
if(tt.whoId!=null && string.valueof(tt.WhoId).startsWith('00Q'))


List<Lead> cnts= new List<Lead>();

Integer number1=1;
for(Lead ct:[select id, name, count1__c from Lead where id in:Leadids])

if(ct.count1__c ==null)



update cnts;
My issue is when i am trying to del a task on lead page it generates this error.
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateLeadCounts caused an unexpected exception, contact your administrator: UpdateLeadCounts: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateLeadCounts: line 4, column 1". 
What you need to know to resolve the issue is :  Trigger.new is not available when the operation is delete.

Use this to get the list of Task then do your stuff using this list.
List<Task> tasks = Trigger.isDelete ? Trigger.old : Trigger.new;