Saturday, July 21, 2012

Too many script statements: 200001 in Salesforce

When you get this error, it is a good indication that your code is in an infinite loop.
See the code below:

                                   
    public integer i {get{if(i==null)
                            i = 3;
                            return i;}set;}
                           
    public List la {get{if(la == null)
                                    {
                                    la = new List();
                                    WHILE(i>0)
                                        {
                                        attachment a = new attachment();
                                        la.add(a);
                                        }
                                    } return la; } set; }
                                  


The i in the WHILE loop does not decrease and stay in its current value which is greater than zero (i>0)  which makes the loop goes on and on until it executed more than 20000 lines of apex statements and hit the governor limits.

To resolve the above example, you simply decrement the value of i in every loop to reach zero at one point. 

                                   
    public integer i {get{if(i==null)
                            i = 3;
                            return i;}set;}
                           
    public List la {get{if(la == null)
                                    {
                                    la = new List();
                                    WHILE(i>0)
                                        {
                                        i--;
                                        attachment a = new attachment();
                                        la.add(a);
                                        }
                                    } return la; } set; }

No comments:

Post a Comment

Did this help you?
How can we help you further?