+ Start a Discussion
Jim MontgomeryJim Montgomery 

too many soql queries error inserting accounts

Not sure where this is erroring out.

trigger MSAData on Account (before insert,before update) { 
    for (account Acc:trigger.new){       
               
Map<String,String> MSALookup = new Map<String,String>();
  Set<String> AZipCode = new Set<String>();  
  
        
if(trigger.IsInsert){
 for(Account A:Trigger.new) {
    AZipCode.add(A.billingpostalcode);
  for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }
 }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
    
    for(Account A:Trigger.new) {    
    AZipCode.add(A.billingpostalcode);
        
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
 }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
else                    

    
if(trigger.isUpdate){    

    for(Account acct:trigger.new){
        account oldAcct= Trigger.oldmap.get(Acct.Id);
    if (oldacct.BillingPostalCode<>Acct.BillingPostalCode){
    
    for(Account A:Trigger.new) {
        
       
           
    AZipCode.add(A.billingpostalcode);
       
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
    }
    
  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
    for(Account A:Trigger.new) {
        
    AZipCode.add(A.billingpostalcode);
  for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }
 }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
}
}
}
}
Best Answer chosen by Jim Montgomery
Waqar Hussain SFWaqar Hussain SF
I got it now.

Please try the below code
 
trigger MSAData on Account (before insert,before update) { 
    //for (account Acc:trigger.new){       
               
Map<String,String> MSALookup = new Map<String,String>();
  Set<String> AZipCode = new Set<String>();  
  
        
if(trigger.IsInsert){
 for(Account A:Trigger.new) {
    AZipCode.add(A.billingpostalcode);
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
    
    for(Account A:Trigger.new) {    
		AZipCode.add(A.billingpostalcode);
	}
	
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
else if(trigger.isUpdate){    

    for(Account acct:trigger.new){
        account oldAcct= Trigger.oldmap.get(Acct.Id);
    if (oldacct.BillingPostalCode<>Acct.BillingPostalCode){
		AZipCode.add(acct.billingpostalcode);
		}
 
    }
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
  
  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
    for(Account A:Trigger.new) {
        
    AZipCode.add(A.billingpostalcode);
  
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
}


//}
}

 

All Answers

arpit vijayvergiyaarpit vijayvergiya
Hello Jim,

You are querying data inside for loop. Remover your query from inside loop. It will help you

Thanks
Jim MontgomeryJim Montgomery
I saw that, but am not sure where to move the SOQL query to in the code.
Waqar Hussain SFWaqar Hussain SF
try below code
trigger MSAData on Account (before insert,before update) { 
    for (account Acc:trigger.new){       
               
Map<String,String> MSALookup = new Map<String,String>();
  Set<String> AZipCode = new Set<String>();  
  
        
if(trigger.IsInsert){
 for(Account A:Trigger.new) {
    AZipCode.add(A.billingpostalcode);
  for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }
 }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
    
    for(Account A:Trigger.new) {    
    AZipCode.add(A.billingpostalcode);
        
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
 }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
else                    

    
if(trigger.isUpdate){    

    for(Account acct:trigger.new){
        account oldAcct= Trigger.oldmap.get(Acct.Id);
    if (oldacct.BillingPostalCode<>Acct.BillingPostalCode){
    
    for(Account A:Trigger.new) {
        
       
           
    AZipCode.add(A.billingpostalcode);
       
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
    }
    
  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
    for(Account A:Trigger.new) {
        
    AZipCode.add(A.billingpostalcode);
  
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
}
}
}
}

 
Jim MontgomeryJim Montgomery
Still getting same error.
Waqar Hussain SFWaqar Hussain SF
Try this one
 
trigger MSAData on Account (before insert,before update) { 
    for (account Acc:trigger.new){       
               
Map<String,String> MSALookup = new Map<String,String>();
  Set<String> AZipCode = new Set<String>();  
  
        
if(trigger.IsInsert){
 for(Account A:Trigger.new) {
    AZipCode.add(A.billingpostalcode);
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
    
    for(Account A:Trigger.new) {    
		AZipCode.add(A.billingpostalcode);
	}
	
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
else                    

    
if(trigger.isUpdate){    

    for(Account acct:trigger.new){
        account oldAcct= Trigger.oldmap.get(Acct.Id);
    if (oldacct.BillingPostalCode<>Acct.BillingPostalCode){
    
    for(Account A:Trigger.new) {
        
       
           
    AZipCode.add(A.billingpostalcode);
 
    }
    
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
  
  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
    for(Account A:Trigger.new) {
        
    AZipCode.add(A.billingpostalcode);
  
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
}
}
}
}

 
Jim MontgomeryJim Montgomery
same error
Waqar Hussain SFWaqar Hussain SF
Can you send me the debug logs. On which line you are getting error?
Jim MontgomeryJim Montgomery
Trigger.MSAData: line 13, column 1 08:27:04.36 (3126975583)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101 Trigger.MSAData: line 13, column 1 08:27:04.127 (3127041740)|CUMULATIVE_LIMIT_USAGE 08:27:04.127 (3127041740)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 101 out of 100 ******* CLOSE TO LIMIT Number of query rows: 100 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 1183 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10
Waqar Hussain SFWaqar Hussain SF
I got it now.

Please try the below code
 
trigger MSAData on Account (before insert,before update) { 
    //for (account Acc:trigger.new){       
               
Map<String,String> MSALookup = new Map<String,String>();
  Set<String> AZipCode = new Set<String>();  
  
        
if(trigger.IsInsert){
 for(Account A:Trigger.new) {
    AZipCode.add(A.billingpostalcode);
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
    
    for(Account A:Trigger.new) {    
		AZipCode.add(A.billingpostalcode);
	}
	
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
}
else if(trigger.isUpdate){    

    for(Account acct:trigger.new){
        account oldAcct= Trigger.oldmap.get(Acct.Id);
    if (oldacct.BillingPostalCode<>Acct.BillingPostalCode){
		AZipCode.add(acct.billingpostalcode);
		}
 
    }
	for(naics_code__c NA:
    [SELECT      zip_code__c, msa_name__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_name__c);
  }
  
  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_description__c = MSALookup.get(A.billingpostalcode);
      }
  }
    for(Account A:Trigger.new) {
        
    AZipCode.add(A.billingpostalcode);
  
 }
 
 for(naics_code__c NA:
    [SELECT      zip_code__c, msa_code__c
     FROM        naics_code__c
     WHERE       zip_code__c IN :AZipCode]) {

    MSALookup.put(NA.zip_code__c,NA.msa_code__c);
  }

  for(Account A:Trigger.new) {
      if(MSALookup.containsKey(A.billingpostalcode)) {
      A.d_B_msa_code__c = MSALookup.get(A.billingpostalcode);
      }
  }
}


//}
}

 
This was selected as the best answer
Jim MontgomeryJim Montgomery
That did it!!

Thanks!!