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
Abhilash DaslalAbhilash Daslal 

Dynamic SOQL used inside a constructor

I want to use 2 constructors within my Controller class, one for a VF component and another for VF page.is it possible to have the same SOQL within 2 constructors.Please give an example
Best Answer chosen by Abhilash Daslal
karthikeyan perumalkarthikeyan perumal
Hello

kindly consider this example 

Class: 
 
public with sharing class C {
    public C() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Zero-constructor called'));
    }
    public C(C controller) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Single-argument C called'));
    }
}


If your page written like this:
<apex:page controller="wrapperAccountOpportunity" ...

Then the zero-parameter constructor will be called.

If your page is written like this:
<apex:page standardController="Account" extensions="wrapperAccountOpportunity" ...

Then the constructor with the single parameter ApexPages.StandardController will be called.
If you want to call fetchQuery  from both constructors, you'll have to call it from both constructors. Only one constructor will ever be called per controller or extension.


And, given the following page:
 
<apex:page controller="C" extensions="C">
    <apex:messages />
</apex:page>
You may expect both constructors to be called, but instead, you'll find only the zero-length (default) constructor is called, since the class is referenced twice. In this sense, classes that become controllers are actually singletons in Visualforce. The controller version of the class is only instantiated once; it can still be constructed normally multiple times within the code itself, but Visualforce will only instantiate one automatically.


Hope its clear.


 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello

kindly consider this example 

Class: 
 
public with sharing class C {
    public C() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Zero-constructor called'));
    }
    public C(C controller) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Single-argument C called'));
    }
}


If your page written like this:
<apex:page controller="wrapperAccountOpportunity" ...

Then the zero-parameter constructor will be called.

If your page is written like this:
<apex:page standardController="Account" extensions="wrapperAccountOpportunity" ...

Then the constructor with the single parameter ApexPages.StandardController will be called.
If you want to call fetchQuery  from both constructors, you'll have to call it from both constructors. Only one constructor will ever be called per controller or extension.


And, given the following page:
 
<apex:page controller="C" extensions="C">
    <apex:messages />
</apex:page>
You may expect both constructors to be called, but instead, you'll find only the zero-length (default) constructor is called, since the class is referenced twice. In this sense, classes that become controllers are actually singletons in Visualforce. The controller version of the class is only instantiated once; it can still be constructed normally multiple times within the code itself, but Visualforce will only instantiate one automatically.


Hope its clear.


 
This was selected as the best answer
Abhilash DaslalAbhilash Daslal
The below code actually worked propoerly as expected.Please tell me if its possible to include dynamic query instead of normal soql query and will it work??


public without sharing class ClsAccountController{
    public List<Account> Accountlist{get; set;}
    public Account acc{get;set;}
    public Id accId{get;set;}
        
    public ClsAccountController(){
        accId = ApexPages.currentPage().getParameters().get('id');
        Accountlist= [Select id,Name,Phone,Email where  
                          Account =: accId];
        
        
    }
    public ClsAccountController(ApexPages.StandardController controller){
        acc = (Account)controller.getRecord();
        Accountlist= [Select id,Name,Phone,Email where  
                          Account =: acc.Id];
        
       
    }

}
karthikeyan perumalkarthikeyan perumal

yes use your Dynamic SOQL for both constructor. it wont throw any exception.