+ Start a Discussion
Tobias HaggeTobias 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.
 
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!
Best Answer chosen by Tobias Hagge
Balaji Chowdary GarapatiBalaji Chowdary Garapati

@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:
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;
	  	
}
Hope it helps.,


Thanks,
balaji
 

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati

@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:
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;
	  	
}
Hope it helps.,


Thanks,
balaji
 
This was selected as the best answer
Tobias HaggeTobias Hagge
That was surprisingly easy. Thanks!