You need to sign in to do that
Don't have an account?
Marc Mastrocola 26
Need Help With Apex Error
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!
Here is the code of the Class:
Here is the Trigger:
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); }
it means that more than 100 queries have been executed. You can only query up to 100 times.
Since "updateLead_NextAction" is inside a loop, it is easy to reach the limit.
Here is a better approach:
try to query all the data and save those in a variable, then "inside updateLead_NextAction", search the needed in the variable.
it is also better to use map-variable for easier searching.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm
Hope this will help you somehow.
All Answers
it means that more than 100 queries have been executed. You can only query up to 100 times.
Since "updateLead_NextAction" is inside a loop, it is easy to reach the limit.
Here is a better approach:
try to query all the data and save those in a variable, then "inside updateLead_NextAction", search the needed in the variable.
it is also better to use map-variable for easier searching.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm
Hope this will help you somehow.