+ Start a Discussion

Apex constructor called twice

Apex constructors seem to be being called twice (on cs9).... Is this normal? Anyone know anything about this?


it is not normal.

from where this class is called?

from VF page or through some class?



The class is called from a VF page. Nothing special. After further investigation, it seems other classes (quite probably all apex classes) are exhibiting the same behaviour. These are not newly written classes but existing pages/classes from an application that has been developed and running over some months. The issue was highlighted after having a Too many script statements: 200001 exception.


I will be checking the production org tomorrow to see if the same thing is occurring.


Can't help feeling this problem must have only started occuring recently (today maybe)....? 


Ok from what I can gather from my own investigation the issue is this... 


If a component has as least one <apex:actionfunction> then the constructor of the apex controller is called twice.

I have written a small example component which creates the condition and confirms that it is happening consistently.

It way well also apply to VF pages but I haven't tested that yet...


To me this looks very much like a Salesforce bug unless anyone can suggest otherwise....?


This has serious consequences, where an apex class is doing a lot of work on construction, this problem effectly halves the govenor limit on script statements... along with a few other worrying scenarios I could probably think of.


It will not call contructure if you have ActionFunction.


It is some different issue.


If possible post the code in VF page and class


I would expect a page with an actionFunction to call the controller constructor twice.  The actionFunction causes an ajax call to the server - a separate request.


I dont think Actionfunction will call constructor untill we make it call.

And even we call action function it will only call the method defined in actionfunction.


One thing would intresting to see if you have and "action" defined on page load? if there is any what is returned by the method.


runger, I'm interested to hear that you say you would expect a page with an actionFunction to call  the controller constructor twice. Does this mean that this really is expected behaviour? Anyone else of the same opinion?


Below is some example code:


public with sharing class example_widget {

	public string searchText { get; set; }
	public example_widget() {
		System.debug('** Constructor: ');
	public PageReference goSearch() {
		PageReference resultPageRef = new PageReference('/apex/example_search');
		resultPageRef.getParameters().put('searchText', searchText);

		return resultPageRef;
	public void doSomething() {
		System.debug('** Action method done');



<apex:component controller="example_widget" allowDML="true">

	<apex:form >
	<apex:actionFunction action="{!doSomething}" name="doSomething" />
	<apex:inputText value="{!searchText}"/>
		<apex:commandButton action="{!goSearch}" value="Go"/>

<apex:page >
	<c:example_widget />


This piece of example code proves the point that when an actionFunction is present in the component, the controller constructor is called twice and when the actionFunction is removed the constructor is only called once. This is not related to when the actionfunction apex method is actually called - this example code doesn't even attempt to call the actionFunction. Just the presence of the actionFunction seems to cause this behaviour.


If this is really expected behaviour, which seems very strange to me, but if it is.... is there any way to prevent constructor code being called twice upon instantiation of the controller? It would seem bizaare not be be able to rely on the constructor being called only once on instantiation.



Hi Nello,


I checked and what you are saying is correct.


but this is correct only in case you are using component. 

if you use code in component with action function in VF page, it wont call controller towice.



Pankaj Raijade.


Anyone ever figure out what is going on here?


I actually just created a very similar post: http://boards.developerforce.com/t5/Apex-Code-Development/Why-is-the-constructor-executing-twice-with-a-component/td-p/432783




To get round this I created the following function: public void getSetupClass() { } and moved all my code from the constructor into there and just put {!SetupClass} immediately after the component delcaration which worked for me - only called once! :)

Chirag MehtaChirag Mehta
After almost 3.5 years, I'm still facing this issue. Seems its not fixed it (https://success.salesforce.com/issues_view?id=a1p30000000RiL8AAK)

Do we still have a solution here? or wingasaurus stated solution of moving everything into init function is only solution as of now ?