+ Start a Discussion
Bayarea 101Bayarea 101 

Why Apex trigger is firing on lead

I have this apex trigger for Task and lead.
trigger UpdateCallCenterLead on Task (after insert, after update) {
List<Task> TId = New List<Task>();
Set<Id> LeadId = New Set<Id>();

List<Lead> UpdatedLead = New List<Lead>();

integer i = 0;    
    for(task t:Trigger.New){   
        if(t.call_disposition__c != null && t.WhoId != null){
            tId.add(t);
            LeadId.add(t.WhoId);}
    }
    Map<ID, Lead> LeadMap = new Map<Id, Lead>([SELECT Id, email, HasOptedOutOfEmail, DoNotCall, Status, Name, Company FROM Lead WHERE Id IN:LeadId]);   
    Lead l;  
    
    for (Task tas:tId){ 
    l = LeadMap.get(tas.WhoId);
        
        if( tas.action_reasons__c == 'Future Need (Potential)'){ 
            l.status = 'Contacted - Future Potential';
            UpdatedLead.add(l);}
            
        if( tas.action_reasons__c == 'Interested – Transferred to BBC'){ 
            l.status = 'Qualified';
            UpdatedLead.add(l);}
            
        if( tas.non_action_reason__c == 'Doing business with existing bank – no need to switch'){ 
            l.status = 'Contacted - No Interest';
            UpdatedLead.add(l);}  
            
        if( tas.non_action_reason__c == 'Other'){ 
            l.status = 'Unqualified';
            UpdatedLead.add(l);}
            
        if( tas.call_disposition__c == 'DO NOT contact again'){ 
            l.status = 'Unqualified';
            l.HasOptedOutOfEmail = TRUE;
            l.DoNotCall = TRUE;
            UpdatedLead.add(l);}
            
        if( tas.call_disposition__c == 'Wrong Contact Info'){ 
            l.status = 'Unqualified';
            UpdatedLead.add(l);}
                    
        if( tas.call_disposition__c == 'Second Attempt- Exhausted'){ 
            l.status = 'Exhausted';
            UpdatedLead.add(l);}

        if( l.status == 'Contacted - Future Potential' && (tas.call_disposition__c == 'Left Message'
        ||tas.call_disposition__c == 'No Answer')){
            l.status = 'Contacted - Future Potential';
            UpdatedLead.add(l);}

       if( l.status != 'Contacted - Future Potential' && (tas.call_disposition__c == 'Left Message'
       ||tas.call_disposition__c == 'No Answer')){
            l.status = 'Contact Attempted';
            UpdatedLead.add(l);}        
            
        If(UpdatedLead.size()>0){
            update UpdatedLead;}                                                 
}
}
if i use ti with jut "after Insert" it works fine but lead status i not update when task is edit.Issue is when i click on lead convert button it generates an error 
System.DmlException: Update failed. First exception on row 0 with id 00TL000000C6BChMAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCallCenterLead: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateCallCenterLead: line 36, column 1: [] (System Code)
any suggestions what am i doing wrong.
KevinPKevinP
trigger UpdateCallCenterLead on Task (after insert, after update) {
List<Task> TId = New List<Task>();
Set<Id> LeadId = New Set<Id>();

List<Lead> UpdatedLead = New List<Lead>();

integer i = 0;    
    for(task t:Trigger.New){   
        if(t.call_disposition__c != null && t.WhoId != null){
            tId.add(t);
            LeadId.add(t.WhoId);}
    }
    Map<ID, Lead> LeadMap = new Map<Id, Lead>([SELECT Id, email, HasOptedOutOfEmail, DoNotCall, Status, Name, Company FROM Lead WHERE Id IN:LeadId]);   
    Lead l;  
    
    for (Task tas:tId){ 
    l = LeadMap.get(tas.WhoId);
        
        if( tas.action_reasons__c == 'Future Need (Potential)'){ 
            l.status = 'Contacted - Future Potential';
            UpdatedLead.add(l);}
            
        if( tas.action_reasons__c == 'Interested – Transferred to BBC'){ 
            l.status = 'Qualified';
            UpdatedLead.add(l);}
            
        if( tas.non_action_reason__c == 'Doing business with existing bank – no need to switch'){ 
            l.status = 'Contacted - No Interest';
            UpdatedLead.add(l);}  
            
        if( tas.non_action_reason__c == 'Other'){ 
            l.status = 'Unqualified';
            UpdatedLead.add(l);}
            
        if( tas.call_disposition__c == 'DO NOT contact again'){ 
            l.status = 'Unqualified';
            l.HasOptedOutOfEmail = TRUE;
            l.DoNotCall = TRUE;
            UpdatedLead.add(l);}
            
        if( tas.call_disposition__c == 'Wrong Contact Info'){ 
            l.status = 'Unqualified';
            UpdatedLead.add(l);}
                    
        if( tas.call_disposition__c == 'Second Attempt- Exhausted'){ 
            l.status = 'Exhausted';
            UpdatedLead.add(l);}

        if( l.status == 'Contacted - Future Potential' && (tas.call_disposition__c == 'Left Message'
        ||tas.call_disposition__c == 'No Answer')){
            l.status = 'Contacted - Future Potential';
            UpdatedLead.add(l);}

       if( l.status != 'Contacted - Future Potential' && (tas.call_disposition__c == 'Left Message'
       ||tas.call_disposition__c == 'No Answer')){
            l.status = 'Contact Attempted';
            UpdatedLead.add(l);}        
            
        If(UpdatedLead.size()>0){
            update UpdatedLead;}                                                 
}
}

First things first, if you use the <> button on the toolbar, you'll be able to share code thats nicely formatted with line numbers.

Line 36, is:
l.status

which i suspect is null, causing your issue. Actualy, to be specific, I suspect L is null. 
Bayarea 101Bayarea 101
any suggestions how to fix this problem
Arunkumar RArunkumar R
Hi Jamil, i just refactored your code, can you try the below one and tell me if you still face issue.
 
trigger UpdateCallCenterLead on Task (after insert, after update)
{
    List<Task> taskList = new List<Task>();
    Set<Id> leadIds = new Set<Id>();
    
    List<Lead> updatedLead = new List<Lead>();
    
    for(task t:Trigger.New)
    {   
        if(t.call_disposition__c != null && t.WhoId != null && t.WhoId.startsWith('00Q'))
        {
            taskList.add(t);
            leadIds.add(t.WhoId);
        }
    }
    
    Map<ID, Lead> LeadMap = new Map<Id, Lead>([SELECT Id, email, HasOptedOutOfEmail, DoNotCall, Status, Name, Company FROM Lead WHERE Id IN:leadIds]);   
    
    for (Task tas:taskList){ 
        Lead l = LeadMap.get(tas.WhoId);
        
        if( tas.action_reasons__c == 'Future Need (Potential)'){ 
            l.status = 'Contacted - Future Potential';
            updatedLead.add(l);}
        
        if( tas.action_reasons__c.equalsIgnoreCase('Interested – Transferred to BBC')){ 
            l.status = 'Qualified';
            updatedLead.add(l);}
        
        if( tas.non_action_reason__c.equalsIgnoreCase('Doing business with existing bank – no need to switch')){ 
            l.status = 'Contacted - No Interest';
            updatedLead.add(l);}  
        
        if( tas.non_action_reason__c.equalsIgnoreCase('Other')){ 
            l.status = 'Unqualified';
            updatedLead.add(l);}
        
        if( tas.call_disposition__c.equalsIgnoreCase('DO NOT contact again')){ 
            l.status = 'Unqualified';
            l.HasOptedOutOfEmail = TRUE;
            l.DoNotCall = TRUE;
            updatedLead.add(l);}
        
        if( tas.call_disposition__c.equalsIgnoreCase('Wrong Contact Info')){ 
            l.status = 'Unqualified';
            UpdatedLead.add(l);}
        
        if( tas.call_disposition__c.equalsIgnoreCase('Second Attempt- Exhausted')){ 
            l.status = 'Exhausted';
            updatedLead.add(l);}
        
        if( l.status.equalsIgnoreCase('Contacted - Future Potential') && (tas.call_disposition__c.equalsIgnoreCase('Left Message') || tas.call_disposition__c.equalsIgnoreCase('No Answer'))){
            l.status = 'Contacted - Future Potential';
            UpdatedLead.add(l);}

        If(UpdatedLead.size()>0){
            update updatedLead;}                                                 
    }
}

 
Bayarea 101Bayarea 101
when i try to save this trigger it generates
 Error: Compile Error: Method does not exist or incorrect signature: [Id].startsWith(String) at line 17 column 64