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
hoomelhoomel 

Huge number of script statements with 2 methods?

Hello,

 

i am currently having some trouble with the governor limits.

 

I wanted to add a new SelectList to my Visualforce page enabling the selection of the month to display and wrote the according methods in my Apex class:

 

 

    public List<Month> monthlist = new Month[36];
    public List<Month> getMonthlist(){
    	for(Integer i=-18;i <= 18;i++){
    		addmonth(i);
    		monthlist.add(month);
    		addmonth(-i);
    	}
    	return monthlist;
    }
    public List<SelectOption> getSelectMonth(){
    	List<SelectOption> SelectMonth = new SelectOption[getMonthList().size()];
    	List<Month> tempMonths = new Month[getMonthList().size()];
    	tempMonths.addAll(getMonthList());
    	System.debug(getMonthList().size());
    	for(Integer i = 0; i < getMonthList().size();i++)
    		{
    			SelectMonth.add(new SelectOption('Hallo','Hallo'));
    			//SelectMonth.add(new SelectOption(tempMonths.get(i).getMonthName() + ' ' + tempMonths.get(i).getYearName(),tempMonths.get(i).getMonthName() + ' ' + tempMonths.get(i).getYearName()));
    		}
    	return SelectMonth;
    }

 

When i now open the page, i get an error regarding the apex scritp statement being exceeded.

 

Before writing the above mentioned methods, the debug log shows this regarding the governor limits:

 

Number of SOQL queries: 4 out of 100
  Number of query rows: 77 out of 10000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 100
  Number of DML rows: 0 out of 10000
  Number of script statements: 727 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10
  Number of find similar calls: 0 out of 10
  Number of System.runAs() invocations: 0 out of 20

 

How is it possible that the code i added uses so much script statements?

I do not understand that at the moment, maybe someone could clear that up for me.

 

If any more code is necessary to find the problem, i will provide it.

 

Regards,

hoomel

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You are generating the entire list of months each time you call your getMonthList function; this results in (at minimum) 217 lines of scripting per call (not including whatever addMonth() does, which is not in your code sample). You should generate the list only once, and reference it as necessary. One small change would greatly increase your performance:

 

 

	public List<Month> monthlist;
	public List<Month> getMonthlist(){
		if(monthlist==null) {
			monthlist = new Month[36];
			for(Integer i=-18;i <= 18;i++){
		    		addmonth(i);
				monthlist.add(month);
				addmonth(-i);
			}
		}
	    	return monthlist;
	}

This way, we "cache" the results of the function.

 

 

In your original code, the main function requires 217 lines of code plus the twice the number the lines of code for addMonth (which is not included in your script). Your code calls this function at least 5 times, resulting in, at minimum, 1100 lines of script execution. By implementing a "cache" where this information is only used once, you change the dynamics to 218 lines of code plus two times the contents of addMonth for the initial run, and only 2 for each subsequent run, since the entire loop is bypassed entirely. These small changes add up quickly:

 

Old/New

217/218

334/220

551/222

788/224

1005/226

 

 

All Answers

sfdcfoxsfdcfox

You are generating the entire list of months each time you call your getMonthList function; this results in (at minimum) 217 lines of scripting per call (not including whatever addMonth() does, which is not in your code sample). You should generate the list only once, and reference it as necessary. One small change would greatly increase your performance:

 

 

	public List<Month> monthlist;
	public List<Month> getMonthlist(){
		if(monthlist==null) {
			monthlist = new Month[36];
			for(Integer i=-18;i <= 18;i++){
		    		addmonth(i);
				monthlist.add(month);
				addmonth(-i);
			}
		}
	    	return monthlist;
	}

This way, we "cache" the results of the function.

 

 

In your original code, the main function requires 217 lines of code plus the twice the number the lines of code for addMonth (which is not included in your script). Your code calls this function at least 5 times, resulting in, at minimum, 1100 lines of script execution. By implementing a "cache" where this information is only used once, you change the dynamics to 218 lines of code plus two times the contents of addMonth for the initial run, and only 2 for each subsequent run, since the entire loop is bypassed entirely. These small changes add up quickly:

 

Old/New

217/218

334/220

551/222

788/224

1005/226

 

 

This was selected as the best answer
hoomelhoomel

Thank you very much, that did the trick!