in the below code i want to update records in employee deatails using data loader after updating in employee details then employee information must update. but it is throwing error exceed maximum number of soql queries.give me suggestions.


   list<employee_information__c>ee=new list<employee_information__c>();

   for(employee_details__c e:trigger.new)


   list<employee_information__c>ei=[select name,employee_lastname__c,employee_phone__c from employee_information__c where employee_id__c=:e.id__c];

   for(employee_information__c ef:ei)
  ef.employee_lastname__c =e.Last_Name__c;

update ee; }   
  • you have queries in a loop. That's very bad coding
    – Novarg
    Feb 10, 2015 at 9:43
  • iam new to sales force can u give me any suggestions to update the code?
    – mahesh
    Feb 10, 2015 at 9:46
  • There are answers below, but you might want also want to check out the Bulk Apex Triggers unit in Trailhead to learn more: developer.salesforce.com/trailhead/… Feb 10, 2015 at 22:09

2 Answers 2


You have a loop in your trigger, that causes the "too many queries" error !

Trying something like that :

  1. Create a list with all items to update

  2. Query all of them

  3. Update them

    Then, you only have 1 query, no matter the number of line you have updated with DataLoader

  • but variable 'e' is in for loop we cant access that from out the loop?
    – mahesh
    Feb 10, 2015 at 10:11
  • First collect all possible 'e' values, than query all records that match. Then do another iteration to process the results. Feb 10, 2015 at 11:04

You should bulkify your trigger like this:

if(Trigger.isUpdate) {
    List<Employee_information__c> ee = new List<Employee_information__c>();

    Map<Id, List<Employee_information__c>> informationByEmployeeId = new Map<Id, List<Employee_information__c>>();

    // make a single query for all your records in trigger.new
    // process them in a list and put them in a map so you can easily access them later
    for(Employee_information__c ei : [select Employee_id__c, Name, Employee_lastname__c, Employee_phone__c from Employee_information__c where Employee_id__c in Trigger.newMap.keySet()]) {
        List<Employee_information__c> informationList = informationByEmployeeId.get(ei.Employee_id__c);
        if(informationList == null) {
            informationList = new List<Employee_information__c>();
        informationByEmployeeId.put(ei.Employee_id__c, informationList);

    for(Employee_details__c e : Trigger.new) {
        for(Employee_information__c ei : informationByEmployeeId.get(e.id__c)) {
            ei.Name = e.Name;
            ei.employee_lastname__c = e.Last_Name__c;
            ei.employee_phone__c = e.phone_number__c;
    update ee;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .