function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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'))
{

LeadIDs.add(tt.whoID);
}
}

if(LeadIds.isEmpty())
return;
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)
ct.count1__c=0;

ct.count1__c=ct.count1__c+number1;


cnts.add(ct);
}

if(cnts.size()>0)
{
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". 
 
BalajiRanganathanBalajiRanganathan
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;