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
VishwanathVishwanath 

"System.LimitException: Too many script statements: 200001 'Extarnal entry point" in test class

HI,

Please any one, help me to solve this issue

 

"System.LimitException: Too many script statements: 200001  'Extarnal entry point" in test class

this error is occure when im going to run the test class

 

Controller:

public class paymentinvoicepdf
{
public paymentinvoicepdf(ApexPages.StandardController controller)
{
}
Public List<UOP_Aggregation_Group__c> agroup{get;set;}
public String contname{get;set;}
public String title{get;set;}
public String Email{get;set;}
public String Telephone{get;set;}
public Boolean flag{get;set;} //{get;set;}
public Royalty_Contacts__c rc{get;set;}
public List<Royalty_Contacts__c> rc1{get;set;}
public List<Royalty_Contacts__c> rc2{get;set;}

public PageReference royalcon()
{
Id id=Apexpages.currentpage().getparameters().get('id');
UOP_Royalty_Payment_History__c urp=[select id,name,Process_Unit__r.Account__r.UOP_CA_S_Account_Manager__c,Payment_History_Event__c,Invoice_Number__c,Invoice_Date__c from UOP_Royalty_Payment_History__c where id=:id];
id urpid=urp.Process_Unit__c;
 rc1=[select id,name,contact__r.name,Royalty_Role__c,Title__c,Phone__c,Email__c  from Royalty_Contacts__c where Process_unit__r.id=:urpid and Royalty_Role__c=:'Royalty (Primary)'];
 if(rc1.size()>0)
 {
 flag=true;
 }
 
 rc2=[select id,name,contact__r.name,Royalty_Role__c,Title__c,Phone__c,Email__c  from Royalty_Contacts__c where Process_unit__r.id=:urpid and Royalty_Role__c=:'Royalty (Secondary)' and Royalty_Role__c=:'Royalty (Primary)' ];
 if(rc2.size()>0)
 {
 flag=false;
 }
 /*for(Royalty_Contacts__c r:rc1)
 {
 if(r.Royalty_Role__c=='Royalty (Primary)')
 {
 flag=true;
 contname=r.Contact__r.name;
 title=r.Title__c;
 Email=r.Email__c;
 Telephone=r.Phone__c;
 //rc=[select id,name,contact__r.name,Royalty_Role__c,Title__c,Phone__c,Email__c  from Royalty_Contacts__c where Process_unit__r.id=:urpid and Royalty_Role__c=:'Royalty (Primary)'];
 }
 else if(r.Royalty_Role__c=='Royalty (Secondary)')
 {
 flag=false;
 }
 
 }*/



 Process_Unit__c pu=[select id,name,Aggregation_Group__c from Process_Unit__c where id=:urpid];
 id aid=pu.Aggregation_Group__c;
 
 agroup=[select id,name,(select id,name,Aggregation_Group__c,Unique_Process_Unit_Identifier__c from Process_Units__r) from UOP_Aggregation_Group__c where id=:aid];
 
return null;
}

}

 

test class:

@istest()
public class paymentinvoicepdftest
{
public static testmethod void test()
{
UOP_Royalty_Payment_History__c uopCreat=new UOP_Royalty_Payment_History__c(Payment_History_Event__c='Report');
ApexPages.StandardController accCtrl = new ApexPages.Standardcontroller(uopCreat);


paymentinvoicepdf pay=new paymentinvoicepdf(accCtrl );
Account ac=new Account(name='test');
//ac.SAP_Customer_Info__c=s.id;
insert ac;
Process_Unit__c pu=new Process_Unit__c(name='testAccount',Account__c=ac.id,Status__c='Licensed',Technology_Supplier__c='ABB Lummus',
Units_Fully_Paid_Capacity__c='Accounting Units',Agreement_Status__c='Fully Executed',License_Type__c='Unit',Subject_To_Audit__c='yes',
Current_Royalty_Type__c='Fully Paid',UOM_Capacity__c='BPSD');
insert pu;
UOP_Royalty_Payment_History__c uop=new UOP_Royalty_Payment_History__c(Payment_History_Event__c='Report',Process_Unit__c=pu.id,
Royalty_Rate__c=0.50,Applicable_BLS__c=10,BLS_Base__c=100,UOP_Year__c='2001',Overage__c=11000);
insert uop;
    ApexPages.currentPage().getParameters().put('id',uop.id);

UOP_Royalty_Payment_History__c urp=[select id,name,Process_Unit__r.Account__r.UOP_CA_S_Account_Manager__c,Payment_History_Event__c,Invoice_Number__c,Invoice_Date__c from UOP_Royalty_Payment_History__c limit 1];
List<Royalty_Contacts__c> rc1=[select id,name,contact__r.name,Royalty_Role__c,Title__c,Phone__c,Email__c  from Royalty_Contacts__c where Process_unit__r.id=:urp.id and Royalty_Role__c=:'Royalty (Primary)']; 
 Process_Unit__c pu1=[select id,name,Aggregation_Group__c from Process_Unit__c where id=:pu.id limit 1];
 UOP_Aggregation_Group__c a=new UOP_Aggregation_Group__c(name='test');
 insert a;
 
 List<UOP_Aggregation_Group__c> agroup=[select id,name,(select id,name,Aggregation_Group__c,Unique_Process_Unit_Identifier__c from Process_Units__r) from UOP_Aggregation_Group__c where id=:a.id];
 
pay.royalcon();
}
}

 Give me some suggestions how to solve this issue

 

Thanks,

Vishwa A

IspitaIspita

Hi,

One question before I answer, is the comments for loop - uncommented in your actual code?

As other than the for - loop your code looks pretty concise and I amamazed what is causing the - too many scripts expection ...

Alsoplease do specify when is the called invoked and as the objects get created and modified are there any workflow rules or triggers acting on the involved objects.

 

 

Navatar_DbSupNavatar_DbSup

Hi,

 

There is a governor limits for an apex code script statements of 2, 00,000 which exceeded in your code of 200,000 script statement. You can try by removing this field  Process_Unit__r.Account__r.UOP_CA_S_Account_Manager__c   in your query.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

VishwanathVishwanath

Hi Ispita,

 

yes Ispita as u told, This object have an trigger for update and isert, when i inactive the trigger my test class of this controller  is working fine,but when it active it will displaying error , and the trigger test class is covering 97% of code.

please let me know how come i can solve this issue

 

Here is my trigger:

trigger PayHist_insert_Under_Agr_ProcessUnit on UOP_Royalty_Payment_History__c (before insert,before Update) 
{
 if(Trigger.isInsert)
  {
   if (!TriggerRecurHelp.hasAlreadyCreatedRecursiv())
    {
      set<String> agid=new set<String>();
      set<id> pid=new set<id>();
      id ppid;
      for(UOP_Royalty_Payment_History__c UOP:Trigger.new)
       {
         ppid=UOP.Process_Unit__c;
         agid.add(UOP.Aggregation_Group__c);
         pid.add(UOP.Process_Unit__c);
       }
   
         Process_unit__c pp=[select id,name,Aggregation_Group__r.id from Process_Unit__c where id=:ppid];
         id aagid=pp.Aggregation_Group__r.id;
         UOP_Aggregation_Group__c aa;
         id prid;
         if(pp.Aggregation_Group__r.id!=null)
          {
            aa=[select id,name from UOP_Aggregation_Group__c where id=:aagid];
           prid=aa.id;
          }

      List<Process_Unit__c> pc=new List<PRocess_Unit__c>();
      pc=[select id,name from Process_Unit__c where Aggregation_Group__r.id=:prid and id!=:ppid];
      System.debug('total PU'+pc.size());
      list<UOP_Royalty_Payment_History__c> pay=new List<UOP_Royalty_Payment_History__c>();
      pay=[select id,name,UOP_Year__c,Payment_History_Event__c from UOP_Royalty_Payment_History__c where Process_Unit__c=:ppid]; 
       for(UOP_Royalty_Payment_History__c rh:trigger.new)
        {
         for(UOP_Royalty_Payment_History__c p:pay)
         {
          if(p.UOP_Year__c==rh.UOP_Year__c && p.Payment_History_Event__c==rh.Payment_History_Event__c)
           {
            Trigger.new[0].UOP_Year__c.addError(''+rh.UOP_Year__c+'Report is Created');  
           }
         }
        }

      List<UOP_Royalty_Payment_History__c> insrtpay=new List<UOP_Royalty_Payment_History__c>();
      for(UOP_Royalty_Payment_History__c U:Trigger.new)
        {
         for(Process_Unit__c p:pc)
          {
           insrtpay.add(new UOP_Royalty_Payment_History__c(
           Payment_History_Event__c=U.Payment_History_Event__c,
           Process_Unit__c=p.id,
           UOP_Year__c=U.UOP_Year__c,
           Royalty_Type__c=U.Royalty_Type__c,
           Comments__c=U.Comments__c,
           Applicable_BLS__c=U.Applicable_BLS__c,
           Application__c=U.Application__c,
           Application_Id__c=U.Application_Id__c,
           Capacity_Reporting_Inaccuracy__c=U.Capacity_Reporting_Inaccuracy__c,
           BLS_Base__c=U.BLS_Base__c,
           Created_By_User_Id__c=U.Created_By_User_Id__c,
           Due_Date__c=U.Due_Date__c,
           Invoice_Ammount_For_FA_DA__c=U.Invoice_Ammount_For_FA_DA__c,
           Invoice_Amount__c=U.Invoice_Amount__c,
           Invoice_Date__c=U.Invoice_Date__c,
           Invoice_Number__c=U.Invoice_Number__c,
           Invoice_Paid_Date__c=U.Invoice_Paid_Date__c,
           Last_Modified_By_User_Id__c=U.Last_Modified_By_User_Id__c,
           Mail__c=U.Mail__c,
           New_Fully_Paid_Capacity__c=U.New_Fully_Paid_Capacity__c,
           Overage__c=U.Overage__c,
           Previous_Fully_Paid_Capacity__c=U.Previous_Fully_Paid_Capacity__c,
           Process_Type__c=U.Process_Type__c,
           Purchase_Order_No__c=U.Purchase_Order_No__c,
           Report_Processed_Date__c=U.Report_Processed_Date__c,
           Royalty_Rate__c=U.Royalty_Rate__c,
           Sales_Order_No__c=U.Sales_Order_No__c,
           Throughput__c=U.Throughput__c,
           Units_of_Measure__c=U.Units_of_Measure__c));
          }
         }
      TriggerRecurHelp.sethasAlreadyCreatedRecursiv();
      System.debug('insrtpay'+insrtpay.size());
      if (!insrtpay.isEmpty())
       {
        try
         {
           insert insrtpay;
         }catch(Exception e){}
       }
    }
  }
    
    if(Trigger.isUpdate)
     {
       List<UOP_Royalty_Payment_History__c> royaltyListupdate=new List<UOP_Royalty_Payment_History__c>();
       List<UOP_Royalty_Payment_History__c> royaltyupdate=new List<UOP_Royalty_Payment_History__c>();
       String year;
       String event;
       String ppid;
       id uid;
        for(UOP_Royalty_Payment_History__c u:Trigger.new)
         {
          uid=u.id;
          ppid=u.Aggregation_Group__c;
          year=u.UOP_year__c;
          event=u.Payment_History_Event__c;
         }
            System.debug('ppidvalue='+ppid);
            System.debug('Yearvale='+year);
            System.debug('Yearvale='+year);
    royaltyupdate=[select id,name,Units_of_Measure__c,Royalty_Type__c,Throughput__c,Previous_Fully_Paid_Capacity__c,Overage__c,
    Royalty_Rate__c,Applicable_BLS__c,BLS_Base__c,Invoice_Date__c,Invoice_Amount__c,Invoice_Number__c,Invoice_Ammount_For_FA_DA__c,
    Due_Date__c,Comments__c,Invoice_Paid_Date__c,Mail__c,New_Fully_Paid_Capacity__c,Process_Type__c,Report_Processed_Date__c,
    Sales_Order_No__c,Capacity_Reporting_Inaccuracy__c from UOP_Royalty_Payment_History__c where Aggregation_Group__c=:ppid and Payment_History_Event__c=:event and UOP_Year__c=:year and id!=:uid]; 
    System.debug('royaltyupdatetotal='+royaltyupdate.size());
      for(UOP_Royalty_Payment_History__c u1:Trigger.new)
      {
       for(UOP_Royalty_Payment_History__c rr:royaltyupdate)
       {
        rr.Units_of_Measure__c=u1.Units_of_Measure__c;
        rr.Royalty_Type__c=u1.Royalty_Type__c;
        rr.Throughput__c=u1.Throughput__c;
        rr.Previous_Fully_Paid_Capacity__c=u1.Previous_Fully_Paid_Capacity__c;
        rr.Overage__c=u1.Overage__c;
        rr.Royalty_Rate__c=u1.Royalty_Rate__c;
        rr.Applicable_BLS__c=u1.Applicable_BLS__c;
        rr.BLS_Base__c=u1.BLS_Base__c;
        rr.Invoice_Date__c=u1.Invoice_Date__c;
        rr.Invoice_Amount__c=u1.Invoice_Amount__c;
        rr.Invoice_Number__c=u1.Invoice_Number__c;
        rr.Invoice_Ammount_For_FA_DA__c=u1.Invoice_Ammount_For_FA_DA__c;
        rr.Due_Date__c=u1.Due_Date__c;
        rr.Comments__c=u1.Comments__c;
        rr.Invoice_Paid_Date__c=u1.Invoice_Paid_Date__c;
        rr.Mail__c=u1.Mail__c;
        rr.New_Fully_Paid_Capacity__c=u1.New_Fully_Paid_Capacity__c;
        rr.Process_Type__c=u1.Process_Type__c;
        rr.Report_Processed_Date__c=u1.Report_Processed_Date__c;
        rr.Sales_Order_No__c=u1.Sales_Order_No__c;
        rr.Capacity_Reporting_Inaccuracy__c=u1.Capacity_Reporting_Inaccuracy__c;
        
        royaltyListupdate.add(rr);
        System.Debug('royaltyListupdate='+royaltyListupdate.size());
       }
     }
       try
         {
           update royaltyListupdate;
         }catch(Exception e){}
            
    }
    
}

 

 

 

IspitaIspita

Hi,

Generally one tries to keep the for loops to the minimum in a trigger and in your trigger there are lets say :-

  • in Insert : 2 simple for loops and a nested for loop of 2 fors
  • in update : there are 2 for loops 
  • so in order to minimize this one needs to reduce the for loops through as change in design of your solution.
  • also check in debug logs in insert and update is happening recursively - if that is the case you need to revisit and redesign your trigger

Hope this helps....

VishwanathVishwanath

Hi Ispita,

 

 

Thanks for your peplay

please give some suggenstion how can i reduce the foor loop in my trigger,

 

 

Thanks,

Vishwa A

IspitaIspita

Wherever you are using an array of records try using an :-

  • associative array
  • Also find of a way of eliminating the for loops
  • Also more precise solution can be suggested if you share what the business process is and what you are trying to achieve.
VishwanathVishwanath

Hi Ispita,

 

What im going to achiev is

i have object A <--- B <--- C

A is parent to B, and B is Parent to C

 

assume in A1 records have near about 100 of child record (Ex ; b1,b2,b3,b4,b5.....................)

 

when i insert  c1 child in b1 parent, it should be insert  in all b2,b3,b4,b5..................... (Ex ; b1 <-- c1, b2<--c2, b3<--c3 ,........)

 

and if i update any one it should be update in all

 

 

it will fine as i written in above trigger, problem with this is Too many script statements​: 200001 'Extarnal entry point"

 

please give me some code solution

 

Thanks,

 

Vishwa A

 

 

 

 

 

pumbaapumbaa

Is there any reason that you need to update so many records in your TEST class? I also experience something similar before when I use Dataloader to upload data. My thought is, you could just create a simple TEST class to just update one record but of course have some coverage of your code. When you do bulk uploading, you could set the batch size to a smaller amount. This would take longer time to load but prevent this exception. Hope this helps.