+ Start a Discussion
revathyrevathy 

System.LimitException: Too many SOQL queries: 101 using Dataloader?

I have account trigger see below,i tried to optimize trigger code,no luck?.
i made bold text for error line,through data loader bulk account data inserting then this trigger should be fire.
please help me on this?

trigger GE_LGT_EM_AvoidDuplicateAccount on Account (after insert,after update) {

Map<id, String> AccountMap = new Map<id, String>();
Map<id, String> AccountMapSalesorg = new Map<id, String>();
Map<id, String> AccountMapDivision = new Map<id, String>();
Map<id,account> updateacc = new Map<id,Account>();
set<string> deletedupeaccount = new set<string>();
set<id>newaccountids=new set<id>();
set<id>newduplicate=new set<id>();
List<recordtype> rt=[select id,name from recordtype where sobjecttype='Account' and name='EMEA Account'];

if(trigger.isinsert ){
     for (Account acc :Trigger.new) {
     
                if(acc.GE_LGT_EM_SAP_Customer_Number__c !=null && acc.RecordTypeid==rt[0].id ) {
                AccountMap.put(acc.id,acc.GE_LGT_EM_SAP_Customer_Number__c);//collect all new customernumber
                AccountMapSalesorg.put(acc.id,acc.GE_LGT_EM_Sales_Org__c);
                AccountMapDivision.put(acc.id,acc.GE_LGT_EM_Division__c);
                newaccountids.add(acc.id);//collect all new ids
                system.debug('newaccountids----->'+newaccountids);
                }
            }
        }
   
//find the same customer number already exist or not in system

list<account> Dupeacclist=[SELECT id,Name,GE_LGT_EM_Partner_Function__c,BillingStreet,GE_LGT_EM_Sales_Org__c,GE_LGT_EM_Division__c,BillingCity,BillingState,BillingPostalCode,GE_LGT_EM_CustomerActiveFlag__c,GE_LGT_EM_Tax_Number_1__c,GE_LGT_EM_Tax_Number_2__c,CurrencyIsoCode,GE_LGT_EM_Plant__c,GE_LGT_EM_PF_Type__c,GE_LGT_EM_SAP_Customer_Number__c FROM Account
     WHERE id NOT IN:newaccountids and RecordTypeid=:rt[0].id and
     GE_LGT_EM_SAP_Customer_Number__c IN:AccountMap.values()and
     GE_LGT_EM_Sales_Org__c IN:AccountMapSalesorg.values()and
     GE_LGT_EM_Division__c IN:AccountMapDivision.values()];

     system.debug('already dupe in system newaccountids----->'+Dupeacclist);

 
for(Account c:trigger.new)
{
      for(Account acct:Dupeacclist) {
     
          if(c.RecordTypeid==rt[0].id && c.GE_LGT_EM_SAP_Customer_Number__c ==acct.GE_LGT_EM_SAP_Customer_Number__c && c.GE_LGT_EM_Sales_Org__c==acct.GE_LGT_EM_Sales_Org__c && c.GE_LGT_EM_Division__c==acct.GE_LGT_EM_Division__c) {
                acct.Name=c.Name;
                acct.GE_LGT_EM_Partner_Function__c=c.GE_LGT_EM_Partner_Function__c;
                acct.BillingStreet=c.BillingStreet;
                acct.BillingCity=c.BillingCity;
                acct.BillingState=c.BillingState;
                acct.BillingPostalCode=c.BillingPostalCode;
                acct.GE_LGT_EM_CustomerActiveFlag__c=c.GE_LGT_EM_CustomerActiveFlag__c;
                acct.GE_LGT_EM_Tax_Number_1__c=c.GE_LGT_EM_Tax_Number_1__c;
                acct.GE_LGT_EM_Tax_Number_2__c=c.GE_LGT_EM_Tax_Number_2__c;
                acct.CurrencyIsoCode=c.CurrencyIsoCode;
                acct.GE_LGT_EM_Plant__c=c.GE_LGT_EM_Plant__c;
                acct.GE_LGT_EM_PF_Type__c=c.GE_LGT_EM_PF_Type__c;
                updateacc.put(acct.id,acct);
                newduplicate.add(c.id);
                system.debug('newduplicate----->'+newduplicate);
         }
     }
}

     if(updateacc.size()>0){
            system.debug('----------->update final');
            update updateacc.values(); 
        }
       
if(newduplicate.size()>0){
    GE_LGT_EM_deleteduplicateaccount.Dupdeleteaccount(newduplicate);
    }

}
JeffreyStevensJeffreyStevens
Pretty sure your issue is that when the "update updateacc.values()" line happens - it is firing the update trigger again, in the same invocation.  Then it does the samething again, and again - until it does it 101 times, and you get the too man soql errors

The solution is to set a static variable the first time through, if the static variable is set - the don't do anymore.

For example - here is how I do my "Run ONce technique"...

Run Once technique (useful in triggers)
Create a utilities class/method like this…

Public with sharing class util_utilities {
Public static Boolean alreadyExecuted;
}

Then use like this…
If (util_utilities.alreadyExecuted != true) {
  Util_utilities.alreadyExecuted = true;
  // Other Code to be executed only once…
}