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
Son HSon H 

Capture first Activity of Lead error "Variable does not exist: WhoId

I am new to coding and saw this code to capture the date of the first Activity of a Lead. I keep on receving this issue "Varable does not exist: WhoID"

What do I need to do specifically to fix this?

Here is trigger:
trigger LeadResponseTrigger on Task (before insert, before update) {
    
    Set<Id> leadsToCheck = new Set<Id>();
    for(Task t : Trigger.new) {
        if(t.WhoId != null && String.valueOf(t.WhoId).startsWith('00Q') && t.Status != 'Completed' && t.ActivityDate <= Date.Today()) {
            leadsToCheck.add(t.WhoId);
        
    }

    List<Lead> leadsToUpdate = [Select Id, CreatedDate 
                                From Lead 
                                where ID in :leadsToCheck and First_Activity_Date__c = null AND CreatedDate > 2010-05-31T12:00:00Z ];
    for(Lead l : leadsToUpdate) {

        l.First_Activity_Date__c = System.now();
    }
    update leadsToUpdate;

    }
}
Abhijeet Anand 6Abhijeet Anand 6
Your code is working absolutely fine in my DEV Org. No errors.
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
trigger LeadResponseTrigger on Task (before insert, before update) 
{
    
    Set<Id> leadsToCheck = new Set<Id>();
    for(Task t : Trigger.new) 
	{
        if( t.WhoId != null && String.valueOf(t.WhoId).startsWith('00Q') && t.Status != 'Completed' && t.ActivityDate <= Date.Today() ) 
		{
            leadsToCheck.add(t.WhoId);
		}	
    }

    List<Lead> leadsToUpdate = [Select Id, CreatedDate 
                                From Lead 
                                where ID in :leadsToCheck and First_Activity_Date__c = null AND CreatedDate > 2010-05-31T12:00:00Z ];
	if(leadsToUpdate.size()>0)
	{	
		for(Lead l : leadsToUpdate) 
		{
			l.First_Activity_Date__c = System.now();
		}
		update leadsToUpdate;
	}
    
}
Let us know if this will help you

I found one } issue