1

I have written one trigger and it's been working in Production for the last year. Now I have created one more trigger on same Object(Company__c). There is no SOQL statements. Now validate my new trigger in Production at the time old trigger the validation is failing. I got a following validation error:

Error: Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.DirectCompany: line 8, column 1"

My trigger trigger can handled bulkyfied or not I need to change my trigger?

Old Trigger:

Trigger DirectCompany on Company__c(AFTER INSERT,AFTER UPDATE)  
{

 TRY
 {   
     LIST<Product__c> sapili      = NEW LIST<Product__c>() ;
     LIST<String> orderEstmate = NEW LIST<String>();
     LIST<Product__c> sapiord = [SELECT Id ,Text02__c FROM Product__c WHERE Text02__c !=: '' ORDER BY Id DESC];//LIMIT 50000
     String orderId;

     FOR( Company__c TempOrder  :  TRIGGER.NEW)
     {

          IF(TempOrder.Conform__c == TRUE && TempOrder.Estimate__c.trim() != NULL)
               orderEstmate.add(TempOrder.Estimate__c.trim());
     }            
     LIST<Sub_Product__c> bidproli = [SELECT Id,Name FROM Sub_Product__c WHERE Name IN : orderEstmate];
     FOR( Company__c com : TRIGGER.NEW)
     {      

           IF(com.Conform__c == TRUE && com.Estimate__c != NULL)
           {                                         
               IF(bidproli.SIZE() != 0)  
               {                 
                  String Text2 = '';
                   FOR(Product__c notsapi : sapiord )
                   {
                        Text2 = Text2 + notsapi.Text02__c.trim().substringBefore('|')+', ';
                   }                                    
                   IF(Text2.contains(com.Id))
                   {                         
                       BREAK;
                   }

                   orderId = com.Id;
                   FOR(Sub_Product__c bidpro : bidproli)
                   {                                                  

                        IF(bidpro.Name.Trim().equals(com.Estimate__c.trim())) 
                        {
                                //Here my own logic here
                                sapili.add(sapi);
                                BREAK ;
                        }
                    }
                }  
           }      
     }    
     IF(sapili.size() != 0)
          INSERT sapili;             

 }
 CATCH(EXCEPTION E)
 {
      SYSTEM.DEBUG(E);
 }
}

New Trigger:

Trigger DeleteCompany on Company__c(AFTER INSERT,AFTER UPDATE)
{  
        LIST<Company__c> deletecom  = new  LIST<Company__c>();
        FOR(Company__c order : [SELECT Id,Delete__c FROM Company__c WHERE Id IN : Trigger.NEW])
        {
            IF((order.Delete__c == TRUE))
                deletecom.add(order);
        }   
        IF(deletecom.SIZE() != 0)
            DELETE deletecom;    
}
Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
Ramesh Somalagari
  • 1,066
  • 3
  • 25
  • 37
  • 1
    pls post the other trigger as well. I suspect it does some dml inserts/updates that triggers the new trigger multiple times, e.g. by doing dml inside a loop. – Guy Clairbois May 06 '14 at 08:01
  • I posted new Trigger also it also working code – Ramesh Somalagari May 06 '14 at 08:06
  • The new trigger code you posted looks invalid: deletecom and deleteord? Also Trigger.new contains the same data that you are querying for. – Keith C May 06 '14 at 08:13
  • I see you did some edits, but this still won't compile. An Order__c loop based on a Company__c query.. – Guy Clairbois May 06 '14 at 08:17
  • 2
    Just looking at the trigger it will be difficult/impossible to see whats going wrong. Have you investigated the debug logs? You may see some other triggers firing, workflows etc... Look for any select statements and check the limits stats as you go. – Richard Durrant May 06 '14 at 08:28
  • It's impossible to say how many queries, DMLs, etc. have been made by looking at one trigger. If you really unable to resolve the issue by narrow downing base on your business flow, use Limits class(It's always good to have this). http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm – highfive May 06 '14 at 08:32
  • What if you just run the test classes on production, without doing the deployment? It might already be failing for a while, not necessarily related to the new trigger. – Guy Clairbois May 06 '14 at 08:47
  • Can I Run All test classes in Production Nothing happened right?.Just it shows the overall code coverage and failed test classes only right? – Ramesh Somalagari May 06 '14 at 09:15

1 Answers1

2

Considering this is an After trigger, it wouldn't be a bad idea to add some recursion control in the trigger. Maybe your trigger is executing recursively and therefore hitting the limit.

See this (Taken from the Salesforce KB) http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Class code :

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Trigger code :

trigger updateTrigger on anyObject(after update) {

    if(checkRecursive.runOnce())
    {
    //write your code here            
    }

}
PepeFloyd
  • 8,001
  • 1
  • 31
  • 57