• Marc Mastrocola 26
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Previous employee wrote Trigger/Class and it's causing an issue during lead conversion (See screenshot). Any help on how to fix would be greatly appreciated!
Error Message

Here is the code of the Class:
 
public class Kash_Task {
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
      
        // update related LEAD for this task if applicable
        String LeadPREFIX = Schema.SObjectType.Lead.getKeyPrefix();
        
        if (tasks_new != null) {
            for(integer i=0; i<tasks_new.size(); i++) {
                Task t = tasks_new[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
        
        if (tasks_old != null) {
            for(integer i=0; i<tasks_old.size(); i++) {
                Task t = tasks_old[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = [SELECT Id, Status, OwnerId FROM Lead WHERE Id=:LeadId];        
        
        // set next open task
        List<Task> nextTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Open') ORDER BY ActivityDate ASC LIMIT 1];
        if (!nextTaskList.isEmpty()) {
            Task nextTask = nextTaskList[0];
          l.Next_Action__c = nextTask.Subject;
          l.Next_Action_Date__c  = nextTask.ActivityDate;
        }
        else {
            l.Next_Action__c = null;
          l.Next_Action_Date__c  = null;
        }
        
        // set last completed task
        List<Task> lastTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Completed') ORDER BY ActivityDate DESC LIMIT 1];
        if (!lastTaskList.isEmpty()) {
            Task lastTask = lastTaskList[0];
          l.Last_Action__c = lastTask.Subject;
          l.Last_Action_Date__c  = lastTask.ActivityDate;
        }
        else {
            l.Last_Action__c = null;
          l.Last_Action_Date__c  = null;
        }

        update l;
    }
}

Here is the Trigger:
 
trigger Kash_Task_Trigger on Task (after insert, after update, after delete, after undelete) {
    if (trigger.isInsert || trigger.isUndelete) {
        Kash_Task.processUpdate(trigger.new, null);        
    }
    else if (trigger.isUpdate) {
        Kash_Task.processUpdate(trigger.new, trigger.old);
    }
    else if (trigger.isDelete) {
        Kash_Task.processUpdate(null, trigger.old);
    }


 
Previous employee wrote Trigger/Class and it's causing an issue during lead conversion (See screenshot). Any help on how to fix would be greatly appreciated!
Error Message

Here is the code of the Class:
 
public class Kash_Task {
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
      
        // update related LEAD for this task if applicable
        String LeadPREFIX = Schema.SObjectType.Lead.getKeyPrefix();
        
        if (tasks_new != null) {
            for(integer i=0; i<tasks_new.size(); i++) {
                Task t = tasks_new[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
        
        if (tasks_old != null) {
            for(integer i=0; i<tasks_old.size(); i++) {
                Task t = tasks_old[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = [SELECT Id, Status, OwnerId FROM Lead WHERE Id=:LeadId];        
        
        // set next open task
        List<Task> nextTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Open') ORDER BY ActivityDate ASC LIMIT 1];
        if (!nextTaskList.isEmpty()) {
            Task nextTask = nextTaskList[0];
          l.Next_Action__c = nextTask.Subject;
          l.Next_Action_Date__c  = nextTask.ActivityDate;
        }
        else {
            l.Next_Action__c = null;
          l.Next_Action_Date__c  = null;
        }
        
        // set last completed task
        List<Task> lastTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Completed') ORDER BY ActivityDate DESC LIMIT 1];
        if (!lastTaskList.isEmpty()) {
            Task lastTask = lastTaskList[0];
          l.Last_Action__c = lastTask.Subject;
          l.Last_Action_Date__c  = lastTask.ActivityDate;
        }
        else {
            l.Last_Action__c = null;
          l.Last_Action_Date__c  = null;
        }

        update l;
    }
}

Here is the Trigger:
 
trigger Kash_Task_Trigger on Task (after insert, after update, after delete, after undelete) {
    if (trigger.isInsert || trigger.isUndelete) {
        Kash_Task.processUpdate(trigger.new, null);        
    }
    else if (trigger.isUpdate) {
        Kash_Task.processUpdate(trigger.new, trigger.old);
    }
    else if (trigger.isDelete) {
        Kash_Task.processUpdate(null, trigger.old);
    }


 
Previous employee wrote Trigger/Class and it's causing an issue during lead conversion (See screenshot). Any help on how to fix would be greatly appreciated!
Error Message

Here is the code of the Class:
 
public class Kash_Task {
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
      
        // update related LEAD for this task if applicable
        String LeadPREFIX = Schema.SObjectType.Lead.getKeyPrefix();
        
        if (tasks_new != null) {
            for(integer i=0; i<tasks_new.size(); i++) {
                Task t = tasks_new[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
        
        if (tasks_old != null) {
            for(integer i=0; i<tasks_old.size(); i++) {
                Task t = tasks_old[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = [SELECT Id, Status, OwnerId FROM Lead WHERE Id=:LeadId];        
        
        // set next open task
        List<Task> nextTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Open') ORDER BY ActivityDate ASC LIMIT 1];
        if (!nextTaskList.isEmpty()) {
            Task nextTask = nextTaskList[0];
          l.Next_Action__c = nextTask.Subject;
          l.Next_Action_Date__c  = nextTask.ActivityDate;
        }
        else {
            l.Next_Action__c = null;
          l.Next_Action_Date__c  = null;
        }
        
        // set last completed task
        List<Task> lastTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Completed') ORDER BY ActivityDate DESC LIMIT 1];
        if (!lastTaskList.isEmpty()) {
            Task lastTask = lastTaskList[0];
          l.Last_Action__c = lastTask.Subject;
          l.Last_Action_Date__c  = lastTask.ActivityDate;
        }
        else {
            l.Last_Action__c = null;
          l.Last_Action_Date__c  = null;
        }

        update l;
    }
}

Here is the Trigger:
 
trigger Kash_Task_Trigger on Task (after insert, after update, after delete, after undelete) {
    if (trigger.isInsert || trigger.isUndelete) {
        Kash_Task.processUpdate(trigger.new, null);        
    }
    else if (trigger.isUpdate) {
        Kash_Task.processUpdate(trigger.new, trigger.old);
    }
    else if (trigger.isDelete) {
        Kash_Task.processUpdate(null, trigger.old);
    }