function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Linda 98Linda 98 

System limit execption:cpu time limit exceeded in trigger.

hi i am getting this error in my trigger when i execute 100 records or so.works fine for records less than tht.

 
for(integer c=0;c<dt.size()-1;c++)
                                 {
                                     TDays=TDays+dt[c].daysBetween(dt[c+1]);
                                 }




pls help


 
trigger Accforet on Opportunity (after insert,after update) {
    List<Acc__c> AccproductList =new List<Acc__c>();
    List<Opportunitylineitem> opplinitemlist =new List<Opportunitylineitem>();
    list<opportunitylineitem > oppdate=  new list<opportunitylineitem >();
    List<Acc_Product_Fcst__c> accquery =new List<Acc_Product_Fcst__c>();
    List<date> dt =new List<date>();
    Set<Id> sProductIds = new Set<Id>();
    Set<Id> sAccountIds = new Set<Id>();
    Set<id> saccprodfcstids =new set<Id>();
    Acc__c accpro =new Acc___c();
    string aname;
    Integer i;
    Integer myIntMonth;
    Integer myIntyear;
    Integer myIntdate;
    
    opplinitemlist=[select Id,PricebookEntry.Product2.Name,opp_account__c,Opp_account_name__c,PricebookEntry.Product2.id,
                    quantity,ServiceDate,Acc_Product_Fcst__c  from Opportunitylineitem  
                    WHERE Opportunityid IN :Trigger.newMap.keySet() AND Acc_product_fcst__c=''];
    
    for(OpportunityLineItem oli:opplinitemlist) {
    sProductIds.add(oli.PricebookEntry.Product2.id);
    sAccountIds.add(oli.opp_account__c);
    //saccprodfcstids.add(oli.Acc_Product_Fcst__c);
    System.debug('************oli:'+oli);
    }
    accquery=[select id,Total_Qty_Ordered__c,Last_Order_Qty__c,Last_Order_Date__c,Fcst_Days_Period__c  from Acc__c       
              where Acc__c.product__c In :sproductids and Acc___c.Account__c in :saccountids]; 
              System.debug('*****accquery:'+accquery);
    for(Acc__c apf1 :accquery){
    saccprodfcstids.add(apf1.id);
    }        
    if(saccprodfcstids!=null){
    oppdate=[select servicedate from opportunitylineitem where Acc_Product_Fcst__c IN :saccprodfcstids ];
    
    i =[select count() from  Opportunitylineitem where acc__c in :saccprodfcstids];
    }
    
     System.debug('************Trigger.new:'+trigger.newmap.keyset());
     System.debug('************sproductsIds:'+sProductIds);
     System.debug('************saccountIds:'+sAccountIds );
     System.debug('************saccprodfcstids:'+saccprodfcstids);  
     System.debug('************accquery:'+accquery);
     System.debug('************oppdate:'+oppdate);
     system.debug('************i:'+i);
     for(Opportunity opp :trigger.new)
     {
         if(opp.Stagename=='Closed Won')
         {
                 for(opportunitylineitem opplist:opplinitemlist)
                 {
                     if(!accquery.isempty())
                     {
                         for(opportunitylineitem opldt :oppdate)
                         {
                             string myDate = String.valueOf(opldt);
                             myDate = myDate.substring(myDate.indexof('ServiceDate=')+12);
                             myDate = myDate.substring(0,10);                                    
                             String[] strDate = myDate.split('-');
                             myIntMonth = integer.valueOf(strDate[1]);
                             myIntYear = integer.valueOf(strDate[0]);
                             myIntDate = integer.valueOf(strDate[2]);
                             Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);
                             dt.add(d);
                         }  
                             dt.add(opp.closedate);
                             dt.sort();                   
                             integer TDays=0;
                                 for(integer c=0;c<dt.size()-1;c++)
                                 {
                                     system.debug('*******tdays:'+tdays);
                                     TDays=TDays+dt[c].daysBetween(dt[c+1]);
                                     system.debug('*******tdays:'+tdays);
                                     System.debug('*******dt:'+dt);
                                 }
                                 for(Acc__c apf:accquery)
                                 {
                                     apf.Fcst_Days_Period__c = TDays/i;
                                     apf.Total_Qty_Ordered__c =apf.Total_Qty_Ordered__c +opplist.quantity;
                                     apf.Last_Order_Qty__c=opplist.quantity;
                                     apf.Last_Order_Date__c=opp.CloseDate ;
                                     apf.Fcst_Qty_Avg__c=apf.Total_Qty_Ordered__c/(i+1);
                                     Opplist.Acc_Product_Fcst__c =apf.Id;  
                                 }
                                // update opplist;
                                // update accquery; 
                     }
                      
                                 else{
                                 accpro.Account__c=opplist.opp_account__c;
                                 accpro.product__c=opplist.PricebookEntry.Product2.Id;
                                 accpro.opplineitemid__c=opplist.id;
                                 accpro.Total_Qty_Ordered__c =opplist.quantity;
                                 accpro.Last_Order_Qty__c=opplist.quantity;
                                 accpro.Last_Order_Date__c=opp.CloseDate;
                                 accpro.Fcst_Qty_Avg__c=opplist.quantity;
                                 accpro.Fcst_Days_Period__c=7;
                                 accproductList.add(accpro);
                                 system.debug('********accpro:'+accpro);
                                 system.debug('********accproductlist:'+accproductlist);
                                 
                                 }
                                 insert accproductlist;
                                 }
                                 update opplinitemlist;
                                 update accquery;
             }
         }
        
     }



 
David Catindoy 11David Catindoy 11
Actually it's not a good idea to place your DML and query statement inside a loop. Try executing it in bulk since we always consider the number of DML statements and query statements that we can only use in a single transaction. Force.com platform only allows you to execute 150 DML statements and 100 query statements in a single transaction. Try to refer in this link >> https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm for more info.
Linda 98Linda 98
Thanks David,

I wrote the soql outside for loop only..
but i had update statements inside for loop.

I just removed them outside and stil i get the same error at same lines.
Please help me ..
Its a urgent issue.:(

please
David Catindoy 11David Catindoy 11
Try to create class as an extension of your trigger. It's not a best practice to put your codes inside the trigger. When you already have your class, call them inside your trigger. Just like this:

trigger AccountAfterUpdate on Account (after update) {

    AccountUpdateManager.doAfterUpdate(Trigger.newMap, Trigger.oldMap);
}
Linda 98Linda 98
no luck david...i am still getting the error.

:(