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
TehNrdTehNrd 

Why is the constructor executing twice with a component?

I have an apex component in which the constructor in the controller is exectuting twice. I have made all the pages and classes as simple as possible to reproduce and this is what we have:

 

The Page

<apex:page >	
    <c:myComponent />
</apex:page>

 

The Component

<apex:component controller="MyController" >
    <apex:form >
        <apex:actionFunction name="thisIsMyName" action="{!doSomething}"  />	
    </apex:form>
</apex:component>

 

The Controller

public class MyController {
	
    //Constructor
    public MyController(){
        system.debug('I am a constructor and I am executing multiple times to drive you crazy.');
    }
    
    public void doSomething(){
    	//Blah blah blah
    }

}

 

The Debug Log

23.0 APEX_CODE,DEBUG
18:56:46.028 (28871000)|EXECUTION_STARTED
18:56:46.028 (28903000)|CODE_UNIT_STARTED|[EXTERNAL]|066500000008tEE|VF: /apex/mypage
18:56:46.039 (39198000)|CODE_UNIT_STARTED|[EXTERNAL]|01p50000000DYoB|MyController <init>
18:56:46.039 (39850000)|METHOD_ENTRY|[1]|01p50000000DYoB|MyController.MyController()
18:56:46.039 (39939000)|METHOD_EXIT|[1]|MyController
18:56:46.041 (41245000)|USER_DEBUG|[5]|DEBUG|I am a constructor and I am executing multiple times to drive you crazy.
18:56:46.041 (41308000)|CODE_UNIT_FINISHED|MyController <init>
18:56:46.041 (41830000)|CODE_UNIT_STARTED|[EXTERNAL]|01p50000000DYoB|MyController <init>
18:56:46.041 (41872000)|USER_DEBUG|[5]|DEBUG|I am a constructor and I am executing multiple times to drive you crazy.
18:56:46.041 (41894000)|CODE_UNIT_FINISHED|MyController <init>
18:56:46.071 (71248000)|CODE_UNIT_FINISHED|VF: /apex/mypage
18:56:46.071 (71263000)|EXECUTION_FINISHED

Any ideas why this is happening. It is making me go a little crazy.

 

If I move everything that is in the component directly to the page it will only execute once, as expected, but I need use components.

 

Thanks,

Jason

Devendra@SFDCDevendra@SFDC

Hi,

 

Have you found a workaround for this? Please let us know.

 

I am facing the same issue.

 

Thanks,

Devendra

AsiereikiAsiereiki
I think is because when you enter the page, first is executed the constructor of the main controller of the page and then the constructor of the component.
In this case, is the same constructor.
To avoid this, use a different contructor in the page and in the component
rangambrrangambr
Are you using the same controller in both component and the page? This may be helpful,

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_lifecycle_example.htm