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
karunakarreddy bade 8karunakarreddy bade 8 

updateleadstatus

Hi all

When I am execute this Trigger I am getting an error : Variable does not exist: l.id at line 38 column 43

Trigger updateleadstatus on Task(after insert,after update,after delete,after undelete)
{
    public set<id>setids=new Set<Id>();
    public List<Lead>LeadToBeChanged=new List<Lead>();
    
    
    If(Trigger.isinsert || Trigger.Isupdate  ||Trigger.Isundelete)
    {
    
       for(task t:trigger.new)
       {
       
          if(string.valueof(t.whoid).startswith('00Q'))
          
          setids.add('t.whoid');
    }
    }
    
    
       If( Trigger.Isdelete )
    {
    
       for(task t:trigger.old)
       {
       
          if(string.valueof(t.whoid).startswith('00Q'))
          
          setids.add('t.whoid');
       }
    }
    
    
    If(setids.size()>0)
       {
       
         for(Lead l :[select l.id, l.task_Count__c,(select id from tasks where isclosed=false) from Lead l where id in:setids]);
          {
          LeadToBeChanged.add(new Lead(id=l.id,task_Count__c = l.tasks.size()));
           
       
       update LeadToBeChanged; 
       }}
    
    
   }

 
DeveloperSudDeveloperSud

Hi karunakarreddy,

syntax is not correct here it seems...can u try with the following ?
for(Lead l :[select id, task_Count__c,(select id from tasks where isclosed=false) from Lead where id IN :setids]){
// your code
}



 
BALAJI CHBALAJI CH
Hi Karunakarreddy,

There are couple of things to be notified in the above trigger.
  • There is no need to use Aliases in the SOQL query unless you need to group the data.
  • DML statements cannot be in for loops as there maybe a chance of hitting Governor limits.
Please find below modified trigger:
Trigger updateleadstatus on Task(after insert,after update,after delete,after undelete)
{
    public set<id>setids=new Set<Id>();
    public List<Lead>LeadToBeChanged=new List<Lead>();
    
    If(Trigger.isinsert || Trigger.Isupdate  ||Trigger.Isundelete)
    {
        for(task t:trigger.new)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
                setids.add('t.whoid');
        }
    }
    
    If( Trigger.Isdelete )
    {
        for(task t:trigger.old)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
                setids.add('t.whoid');
        }
    }
    
    If(setids.size()>0)
    {
        for(Lead l :[select id, task_Count__c,(select id from tasks where isclosed=false) from Lead where id in:setids]);
        {
            LeadToBeChanged.add(new Lead(id=l.id,task_Count__c = l.tasks.size()));
        }
    }
    
    if(!LeadToBeChanged.isEmpty())
        update LeadToBeChanged; 
}

Let us know if that helps you.

Best Regards,
BALAJI
Swetha A 5Swetha A 5
Hi Karunakar,

The error is at line no: 26 near the for loop. It is terminated by a semicolon(;). So the execution stops there and not going forward. Remove the semicolon and try to save the code. You will get it executed.

Thanks.
Swetha
karunakarreddy bade 8karunakarreddy bade 8
Hi Balaji,

When I am execute the above Trigger I am getting same error
Error: Variable does not exist: l.id at line 28 column 45
BALAJI CHBALAJI CH
Here find updated Trigger:
Trigger updateleadstatus on Task(after insert,after update,after delete,after undelete)
{
    public set<id>setids=new Set<Id>();
    public List<Lead>LeadToBeChanged=new List<Lead>();
    
    If(Trigger.isinsert || Trigger.Isupdate  ||Trigger.Isundelete)
    {
        for(task t:trigger.new)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
                setids.add('t.whoid');
        }
    }
    
    If( Trigger.Isdelete )
    {
        for(task t:trigger.old)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
                setids.add('t.whoid');
        }
    }
    
    If(setids.size()>0)
    {
        for(Lead l :[select id, task_Count__c,(select id from tasks where isclosed=false) from Lead where id in:setids])
        {
            LeadToBeChanged.add(new Lead(id=l.id,task_Count__c = l.tasks.size()));
        }
    }
    
    if(!LeadToBeChanged.isEmpty())
        update LeadToBeChanged; 
}

 
karunakarreddy bade 8karunakarreddy bade 8
Hi Balaji,

Error: Variable does not exist: whoid at line 10 column 31
BALAJI CHBALAJI CH
Trigger updateleadstatus on Task(after insert,after update,after delete,after undelete)
{
    public set<id>setids=new Set<Id>();
    public List<Lead>LeadToBeChanged=new List<Lead>();
    
    If(Trigger.isinsert || Trigger.Isupdate  ||Trigger.Isundelete)
    {
        for(task t:trigger.new)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
            {
                setids.add(t.whoid);
            }
        }
    }
    
    If( Trigger.Isdelete )
    {
        for(task t:trigger.old)
        {
            if(string.valueof(t.whoid).startswith('00Q'))
                setids.add('t.whoid');
        }
    }
    
    If(setids.size()>0)
    {
        for(Lead l :[select id, task_Count__c,(select id from tasks where isclosed=false) from Lead where id in:setids])
        {
            LeadToBeChanged.add(new Lead(id=l.id,task_Count__c = l.tasks.size()));
        }
    }
    
    if(!LeadToBeChanged.isEmpty())
        update LeadToBeChanged; 
}
Please try above code. If still getting any error., kindly share the screenshot of the error.
 
karunakarreddy bade 8karunakarreddy bade 8
Hi 

please find the screenshot

User-added image
Swetha A 5Swetha A 5
Hi,

Could you please save the code in developer console. And The trigger is on Task object. Have you created the trigger under task object. Try it once in developer console..

Thanks
karunakarreddy bade 8karunakarreddy bade 8
Hi,

in the developer console also it will not working
BALAJI CHBALAJI CH
I can able to save in my Dev org with the same code.
User-added image

Try deleting the existing trigger in your org and create a new one with same above code and also check the version.