+ Start a Discussion
sandeepathadusandeepathadu 

hi help for too many script statements error

hi i have wriiten a code which contains 6 insert statements it is working fine for me in sandbox but when we are doing migration its failing and giving too many script statements please help me resolve this issue

My OwnMy Own

 

 

Can you please post your code here. Do you used any List<object> ? or DML In for ?

sandeepathadusandeepathadu

yes i have used many lists

 

here is my code

public class Dev_From_To_Ctrl
{
public Dev_From_To_Ctrl(ApexPages.StandardController controller) 
{ 
userInputAppointment = new Appointment__c();

}
public Appointment__c userInputAppointment {get; set;}
public list<appointment__c> apps {get;set;}  
public list<appointment__c> apps1 {get;set;}  
public list<appointment__c> apps2 {get;set;}
public list<appointment__c> apps3 {get;set;}
public list<appointment__c> apps4 {get;set;}
public list<appointment__c> apps5 {get;set;}
public list<appointment__c>  getall {get;set;}



/*code starts from here
first scenario----from hour less than to hour   ex:8<9 8<15 
sub scenarios in first scenario
ex: from   8:10  to   12:50

scenario 1)8:10 to 8:50
scenario 2)9:00 to 9:50 & 10:00 to 10:50 &11:00 to 11:50
scenario 3)12:00 to   12:50*/




public PageReference save()
{
apps = new list<appointment__c>();
apps1 = new list<appointment__c>();
apps2 = new list<appointment__c>();
apps3 = new list<appointment__c>();
apps4 = new list<appointment__c>();
apps5 = new list<appointment__c>();
string j=userInputAppointment.Appt_24_From_Min__c;
string v =userInputAppointment.Appt_24_To_Min__c;
integer i=integer.valueof(userInputAppointment.Appt_24_From_Hour__c);          
integer i1=integer.valueof(userInputAppointment.Appt_24_To_Hour__c);
integer i2=integer.valueof(userInputAppointment.Appt_24_From_Min__c);
integer i3=integer.valueof(userInputAppointment.Appt_24_To_Min__c);

if(i<i1)
{ 

//scenerio 1 i.e 8:10 to 8:50 stars here
for(integer k=0;k<(60-i2)/10;k++)
{
//first take the given values by user



appointment__c ap = new appointment__c();
ap.Appt_24_From_Hour__c=String.valueOf(i);
ap.Appt_24_To_Hour__c=String.valueOf(i1);
ap.Appt_24_From_Min__c=String.valueOf(j);
ap.Appt_24_To_Min__c=String.valueOf(v);
ap.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap.branch__c =  userInputAppointment.branch__c;
ap.account__c = userInputAppointment.Account__c;
ap.doctor__c=userInputAppointment.doctor__c;
ap.appt_date__c = userInputAppointment.appt_date__c;
ap.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap.Appt_AM_PM__c= 'PM';
}
ap.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap.Appt_Minute__c = userInputAppointment.Appt_24_From_Min__c;
ap.RecordTypeid=userInputAppointment.RecordTypeid;
if(i2!=integer.valueof(userInputAppointment.Appt_24_From_Min__c))
{
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap.RecordTypeid='012N0000000CgZ2';
       
}
apps1.add(ap);
}
insert apps1[0];


// here the first record in the list

userInputAppointment.Appt_24_From_Min__c=String.valueOf(i2);
for(integer k=0;k<(50-i2)/10;k++)
{
appointment__c ap8 = new appointment__c();

ap8.Appt_24_From_Hour__c=String.valueOf(i);
ap8.Appt_24_To_Hour__c=String.valueOf(i1);
ap8.Appt_24_From_Min__c=String.valueOf(j);
ap8.Appt_24_To_Min__c=String.valueOf(v);
ap8.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap8.branch__c =  userInputAppointment.branch__c;
ap8.account__c = userInputAppointment.Account__c;
ap8.doctor__c=userInputAppointment.doctor__c;
ap8.appt_date__c = userInputAppointment.appt_date__c;
ap8.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap8.Appt_AM_PM__c= 'PM';
}
ap8.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap8.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap8.Appt_Minute__c = String.valueOf( integer.valueof(userInputAppointment.Appt_24_From_Min__c)+10);
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap8.RecordTypeid='012N0000000CgZ2';
       
ap8.Dev_Parent_Appt__c=apps1[0].id;
apps4.add(ap8);
userInputAppointment.Appt_24_From_Min__c=String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Min__c)+10);
}
insert apps4;

//scenerio 1 i.e 8:10 to 8:50 ends here


//scenario 2 starts here   i.e  9:00 to 9:50 & 10 to 10:50 &11:00 to 11:50




for( integer l=1;l<i1-i;l++)
{
userInputAppointment.Appt_24_From_Hour__c=String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)+1);
string x='00';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)<integer.valueof(userInputAppointment.Appt_24_To_Hour__c))
{
for(integer g=0;g<6;g++)
{
appointment__c ap1=new appointment__c();
ap1.Appt_24_From_Hour__c=String.valueOf(i);
ap1.Appt_24_To_Hour__c=String.valueOf(i1);
ap1.Appt_24_From_Min__c=String.valueOf(j);
ap1.Appt_24_To_Min__c=String.valueOf(v);
ap1.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap1.Appt_24_From_Hour__c=userInputAppointment.Appt_24_From_Hour__c;
ap1.Appt_24_To_Hour__c=userInputAppointment.Appt_24_To_Hour__c;
ap1.Appt_24_From_Min__c=userInputAppointment.Appt_24_From_Min__c;
ap1.Appt_24_To_Min__c=userInputAppointment.Appt_24_To_Min__c;
ap1.branch__c =  userInputAppointment.branch__c;
ap1.account__c = userInputAppointment.Account__c;
ap1.appt_date__c = userInputAppointment.appt_date__c;
ap1.doctor__c=userInputAppointment.doctor__c;
ap1.Appt_AM_PM__c= 'AM';
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap1.RecordTypeid='012N0000000CgZ2';
       
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap1.Appt_AM_PM__c= 'PM';
}
ap1.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap1.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap1.Appt_Minute__c=x;
ap1.Dev_Parent_Appt__c=apps1[0].id;
apps2.add(ap1);
x=String.valueOf(integer.valueof(x)+10);
}
}
}
insert apps2;

//scenario 2 ends here   i.e  9:00 to 9:50 & 10 to 10:50 &11:00 to 11:50


//scenario 3  i.e 12:00 to   12:50 stars here






string q='00';


userInputAppointment.Appt_24_From_Hour__c=String.valueOf(i+(i1-i));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)==integer.valueof(userInputAppointment.Appt_24_To_Hour__c))
{
for(integer b=0;b<((i3/10));b++)
{
appointment__c ap3=new appointment__c();
ap3.Appt_24_From_Hour__c=String.valueOf(i);
ap3.Appt_24_To_Hour__c=String.valueOf(i1);
ap3.Appt_24_From_Min__c=String.valueOf(j);
ap3.Appt_24_To_Min__c=String.valueOf(v);
ap3.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap3.account__c = userInputAppointment.Account__c;
ap3.branch__c =  userInputAppointment.branch__c;
ap3.doctor__c=userInputAppointment.doctor__c;
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap3.RecordTypeid='012N0000000CgZ2';
       
ap3.appt_date__c = userInputAppointment.appt_date__c;
ap3.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap3.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap3.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap3.Appt_AM_PM__c= 'PM';
}
ap3.Dev_Parent_Appt__c=apps1[0].id;
ap3.Appt_Minute__c=q;
ap3.Dev_Parent_Appt__c=apps1[0].id;
apps3.add(ap3);
q=String.valueOf(integer.valueof(q)+10);
}
}  
}
insert apps3;

//scenario 3  i.e 12:00 to   12:50 ends here








//case 2   i.e if from hour equals 2 to hour   
//ex:8:10  to   8:30 or 8:10   to 8:50

//now 

if(i==i1)
{
for(integer z=0;z<((i3-i2)/10);z++)
{
appointment__c ap4=new appointment__c();
ap4.Appt_24_From_Hour__c=String.valueOf(i);
ap4.Appt_24_To_Hour__c=String.valueOf(i1);
ap4.Appt_24_From_Min__c=String.valueOf(j);
ap4.Appt_24_To_Min__c=String.valueOf(v);
ap4.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap4.account__c = userInputAppointment.Account__c;
ap4.branch__c =  userInputAppointment.branch__c;
ap4.doctor__c=userInputAppointment.doctor__c;
ap4.appt_date__c = userInputAppointment.appt_date__c;
ap4.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c));
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap4.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap4.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap4.Appt_AM_PM__c= 'PM';
}
ap4.RecordTypeid=userInputAppointment.RecordTypeid;
if(i2<integer.valueof(userInputAppointment.Appt_24_From_Min__c))
{
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap4.RecordTypeid='012N0000000CgZ2';
       
}
ap4.Appt_Minute__c = userInputAppointment.Appt_24_From_Min__c;
apps.add(ap4);
userInputAppointment.Appt_24_From_Min__c=String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Min__c)+10);
}
insert apps[0];
userInputAppointment.Appt_24_From_Min__c=String.valueOf(i2);
for(integer c=0;c<((i3-i2)/10)-1;c++)
{
appointment__c ap9 = new appointment__c();
ap9.Appt_24_From_Hour__c=String.valueOf(i);
ap9.Appt_24_To_Hour__c=String.valueOf(i1);
ap9.Appt_24_From_Min__c=String.valueOf(j);
ap9.Appt_24_To_Min__c=String.valueOf(v);
ap9.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap9.branch__c =  userInputAppointment.branch__c;
ap9.doctor__c=userInputAppointment.doctor__c;
ap9.account__c = userInputAppointment.Account__c;
ap9.appt_date__c = userInputAppointment.appt_date__c;
ap9.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>=12)
{
ap9.Appt_AM_PM__c= 'PM';
}
ap9.Appt_Hour__c =userInputAppointment.Appt_24_From_Hour__c;
if(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)>12)
{
ap9.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Hour__c)-12);
}
ap9.Appt_Minute__c = String.valueOf( integer.valueof(userInputAppointment.Appt_24_From_Min__c)+10);
//LIVE ID
       // app1.RecordTypeid='01290000000ZXNL';
       
       //SB ID
       ap9.RecordTypeid='012N0000000CgZ2';
       
ap9.Dev_Parent_Appt__c=apps[0].id;
apps5.add(ap9);
userInputAppointment.Appt_24_From_Min__c=String.valueOf(integer.valueof(userInputAppointment.Appt_24_From_Min__c)+10);
}
insert apps5;


}
getall = [Select id,name,recordtypeid,Doctor__c,Branch__c,Account__c,Appt_Date__c,Appt_24_From_Hour__c,Appt_24_From_Min__c
,Appt_24_To_Hour__c,Appt_24_To_Min__c,Appt_Time__c  from Appointment__c where id IN: Apps or id IN : apps1 or id IN : apps2 or id IN : apps3 or id IN : apps4 or id IN : apps5 ]; 

//return null;
return page.Dev_From_To_Table;

}


}

 

Rahul SharmaRahul Sharma

As I see your code is not that huge to get Such an error,

I wold suggest you to Initialize an object As 2nd method:

 

1)
Account a = new Account(); a.Name = 'Test Account - 01'; a.Phone = 12345; update a;
2)
Account a = new Account(Name = 'Test Account - 01', Phone = 12345); update a;

 That will reduce much of script statements.

My OwnMy Own

 

 

I have observed that in your code your using reduent code, instead of that use function with parameters. which will reduce the duplicate code.  

 

This code is repeated in all the conditions. 

appointment__c ap1=new appointment__c();
ap1.Appt_24_From_Hour__c=String.valueOf(i);
ap1.Appt_24_To_Hour__c=String.valueOf(i1);
ap1.Appt_24_From_Min__c=String.valueOf(j);
ap1.Appt_24_To_Min__c=String.valueOf(v);
ap1.Dev_Appt_Type_Normal_Or_From_To__c   =true;
ap1.Appt_24_From_Hour__c=userInputAppointment.Appt_24_From_Hour__c;
ap1.Appt_24_To_Hour__c=userInputAppointment.Appt_24_To_Hour__c;
ap1.Appt_24_From_Min__c=userInputAppointment.Appt_24_From_Min__c;
ap1.Appt_24_To_Min__c=userInputAppointment.Appt_24_To_Min__c;
ap1.branch__c =  userInputAppointment.branch__c;
ap1.account__c = userInputAppointment.Account__c;
ap1.appt_date__c = userInputAppointment.appt_date__c;
ap1.doctor__c=userInputAppointment.doctor__c;
ap1.Appt_AM_PM__c= 'AM';

 

 

or else just follow as Rahul suggested.