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
AlanisticAlanistic 

Controller extension for accounts to pull back contacts

Im new to controller extensions and I'm not finding the SF help pages particularly helpful in this area.

I have a VF page that shows account information.  I need to also include the contacts associated with the account.

My Extension is below:

public class myAccountControllerExtension {

    public Account account { get; }
    public Contact contact { get; }
   
    public myAccountControllerExtension(){
        contact = null;
        getContacts();
    }
   
    private void getContacts(){
        List<Account> accounts = new List<Account>([
            SELECT Id, Name, AccountNumber,          
                 (SELECT Id, Name FROM Contacts WHERE Portal_User__c = true)
            FROM Account]);
           
    }
}



When I reference the extension in my page I receive the error: 

Error: Unknown constructor 'myAccountControllerExtension.myAccountControllerExtension(ApexPages.StandardController controller)'

Can anyone advise why?

Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because whenever you both Standard Controller and an custom controller using extension attribute then you need to provide a Override constructor for your Controller.
Replace you constructer with below code
public myAccountControllerExtension(ApexPages.StandardController controller){
        contact = null;
        getContacts();
}

Please mark it as best solution to your problem if it does solve your problem.
AshlekhAshlekh
Hi,

public class myAccountControllerExtension {

    public Account acct { get; }
    public List<Contact> cont { get; }
   
	
	public myAccountControllerExtension(ApexPages.StandardSetController controller) {
        this.acct = (Account)controller.getRecord(); 
		getContacts(acct.id);
    }  

	public void getContacts(String aid){
		if(aid != null && aid instanceOf Id)
		{
			for(Account a : [select id,name,AccountNumber ,(SELECT Id, Name FROM Contacts WHERE Portal_User__c = true) FROM Account where id =:aid])
			{	
				if(a.contacts!=null && a.contacts.size()>0)
				cont = a.contacts;
			}
		}
	}
	
}

IF it helps you than please mark it as a solution and ENJOY APEX
AlanisticAlanistic
Thanks to both of you.  That makes sense, however I now receive:

Compile Error: Variable is not visible: acct at line 7 column 9


James LoghryJames Loghry
That's because D-Horse forgot to add a set; in his variable declartion.

Change:

public Account acct { get; }

To

public Account acct { get; set; }

Note, most of the examples on here are written off the top of our heads, so they'll likely contain compile errors like this.  
AlanisticAlanistic
That now compiles, however the VF page now displays:

Error: Unknown constructor 'myAccountControllerExtension.myAccountControllerExtension(ApexPages.StandardController controller)'
James LoghryJames Loghry
Looks like D-Horse added the wrong constructor as well.. try the following:

public class myAccountControllerExtension {
    public Account acct { get; set; }
    public List<Contact> cont { get; }

    public myAccountControllerExtension(ApexPages.StandardController controller) {

        this.acct = (Account)controller.getRecord();
        getContacts(acct.id);
    } 

    public void getContacts(String aid){
        if(aid != null && aid instanceOf Id){
            for(Account a : [select id,name,AccountNumber ,(SELECT Id, Name FROM Contacts WHERE Portal_User__c = true) FROM Account where id =:aid]) {  
                if(a.contacts!=null && a.contacts.size()>0)
                    cont = a.contacts;
            }
        }
    }
}


AlanisticAlanistic
I think I'm almost there:

Error: Unknown property 'AccountStandardController.contact'

This is coming on my VF page:


<apex:page standardController="Account" extensions="myAccountControllerExtension">

<apex:pageBlock title="Contacts">
          {!contact.Name}
          
        </apex:pageBlock>
Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because you have no property with named "contact " in your controller.
Use below code if you want to display all contact associated with an account.

<apex:pageBlockTable value="{!cont}" var="con" >
    {!con.Name}
</apex:pageBlockTable>
AlanisticAlanistic
That compiles ok but my contact list is blank.

<apex:pageBlock title="Contacts" >
         <apex:pageBlockTable value="{!cont}" var="con" >
            {!con.id}
            {!con.Name}
</apex:pageBlockTable>
            
        </apex:pageBlock>


If I change this to:

<apex:pageBlock title="Contacts" >
            {!cont}
        </apex:pageBlock>

I receive a list of IDs which do correspond to contacts.  Why is the pageblocktable not showing anything?
AlanisticAlanistic
Does anyone have any further suggestions?