• Tyler Darnell
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello,

I wrote a trigger which is working fine in development. I am able to update accounts and peform account update dataloads without any issues. I deployed to production where I'm able to mannualy update accounts however I'm receiving the below error when I attempt an account update dataload.

System.LimitException: Too many SOQL queries: 101

I belive my trigger is offending the following trigger best pratice. Please suggest to me how to fix this issue.
5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception
 
trigger trgAccount_CustomNAICS on Account (before insert, before update) {

    for(Account acc:trigger.new)
    {        
        IF (String.isBlank(acc.NAICS_Code__c) && String.IsNotBlank(acc.NaicsCode))
        {
            try {
            System.debug('IF###AccountID: ' + acc.id);
            
            list <NAICS__c> listNaics=[select id from NAICS__c where name =:acc.NaicsCode LIMIT 1];
            
            System.debug('NAICS__c Name: ' + listNaics[0].id);
            
            acc.NAICS_Code__c = listNaics[0].id;
            }
        catch(System.Exception e){ System.debug('Exception occured: ' + e); }     
        }
        Else
        {
        System.debug('ELSE###AccountID: ' + acc.id);
        System.debug('NAICS_Code__c: ' + acc.NAICS_Code__c);
        System.debug('NaicsCode: ' + acc.NaicsCode);
        }
    }
    
}