You need to sign in to do that
Don't have an account?
Tobias Hagge
Task batch update - System.Limit Exception Too Many SOQL Queries 101
Hello. I hope you can help me debugging the following code. The scenario is that 200 leads are being updated in a batch with tasks being created on them. The task has a name field (Agent_Name__c) that I want to compare against a name field (Name) on a custom object (Acquirer__c) and then insert the custom object ID in a lead custom lookup field (Sales_Agent__c). Unfortunately the governor limit is being hit and I can't figure out which part does too many queries.
Thanks for the help!
trigger Trigger_Task2Lead on Task (after insert) { Task[] tasks = Trigger.New; Map<String, Acquirer__c> acquirers = new Map<String, Acquirer__c>(); for(Acquirer__c a: [SELECT Name, Id from Acquirer__c]) { acquirers.put(a.Name, a); } Map<String, Task> leadMap = new Map<String, Task>(); for(Task task: tasks) { String id = task.WhoId; if(id != null && id.startsWith('00Q')==TRUE) // All Lead IDs begin with 00Q leadMap.put(task.WhoId, task); List<Lead> leads = new List<Lead>([Select Id, Sales_Agent__c from Lead where Id in :leadMap.keySet()]); for(Lead lead: leads) { String agentname = leadMap.get(lead.Id).Agent_Name__c; Acquirer__c acquirer = acquirers.get(agentname); try { lead.Sales_Agent__c = acquirer.Id; } catch (NullPointerException e){ System.debug('NPE***'); } } update leads; } }
Thanks for the help!
@Tobias Hagge:
You have the correct way of writing the code, but only thing you missed is., closing the second for loop incorrectly which happened to have the lead query inside for loop, i just changed the position of closing parenthesis of second for loop., Try the below code: Hope it helps.,
Thanks,
balaji
All Answers
@Tobias Hagge:
You have the correct way of writing the code, but only thing you missed is., closing the second for loop incorrectly which happened to have the lead query inside for loop, i just changed the position of closing parenthesis of second for loop., Try the below code: Hope it helps.,
Thanks,
balaji