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
mohit tripathimohit tripathi 

Basic Custom Controller

Hi Guys

When I Am trying to fetch contact's Record from Account via custom controller, I Am unable to fetch contact's record from an account in vf page.
I feel controller is fine but I Am missing something in VF page.

Here is my controller-

public class AccCustomOppController {

    Public Account Acct {get; set;}

public AccCustomOppController (){

String AcctId = ApexPages.CurrentPage().getParameters().get('id');

Acct= [select name, Rating,  id from Account Where Id =: AcctId];

}
public List<Contact> ContactList{
get{

ContactList = [ select name, Email , Department from Contact where AccountId = :Acct.Id ];

return ContactList;
}
set;
}

Public PageReference Save(){

update Acct;
return new pageReference ('/' + Acct.ID);

}
}

and VF page is-

<apex:page controller="AccCustomOppController">
<apex:form >
<apex:pageblock Title="Account Custom Controller check">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save the updated Account Information"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!Acct.Name}"/>
</apex:pageBlockSection>
</apex:pageblock>

<apex:pageBlock>
<apex:pageBlockTable value="{!ContactList}" var="Con">
<apex:column value="{!con.Name}" title="Contact Name"/>
</apex:pageBlockTable>
</apex:pageBlock>


</apex:form>
</apex:page>


Please let me know what am I doing Wrong.


Thanks in Advance
Best Answer chosen by mohit tripathi
Ronty AhmadRonty Ahmad
Hi Mohit
Mainly in your <apex:pageBlockTable value="{!ContactList}" var="Con"> you are using Con with capital C but in <apex:column value="{!con.Name}" title="Contact Name"/> you are using lower con.
just change var="Con" to var="con".  your all ohter code is fine.
 
<apex:pageBlockTable value="{!ContactList}" var="Con">
<apex:column value="{!con.Name}" title="Contact Name"/>
</apex:pageBlockTable>

Thanks

Ronty
 

All Answers

Devanshu soodDevanshu sood
<apex:page controller="AccCustomOppController">
    <apex:form >
    
    <apex:pageblock Title="Account Custom Controller check">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save the updated Account Information"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:inputField value="{!Acct.Name}"/>
        </apex:pageBlockSection>
    </apex:pageblock>
    
    <apex:pageBlock >
        <apex:repeat value="{!ContactList}" var="con">
        
            <apex:commandLink value="{!con.Name}" title="Contact Name"/>  <br></br>
         
        </apex:repeat> 
    </apex:pageBlock>
    
    
    </apex:form>
</apex:page>

Use this code it's working fine, prints all it's related contacts.
Thanks 
Devanshu
Ronty AhmadRonty Ahmad
Hi Mohit
Replace  your  ContactList  method  on your AccCustomOppController 
 
public List<Contact> ContactList{
get{

ContactList = [ select name, Email , Department from Contact where AccountId = :Acct.Id ];

return ContactList;
}
set;
}
 To 
 
Public List<Contact> ContactList {
            get{
               return [ select name, Email , Department from Contact where AccountId = :Acct.Id ];
              
               }
            set;
         }
<apex:pageBlock title="Contact">
          <apex:outputPanel id="contactDetails">
             <apex:pageBlockTable value="{!ContactList}" var="con" id="conlist" title="Contact">
                 <apex:column value="{!con.Name}"/>
                 <apex:Column value="{!con.Department}" />
                 <apex:Column value="{!con.Email}" />
             </apex:pageBlockTable>
         </apex:outputPanel>
       </apex:pageBlock>


Thanks
Ronty
Ronty AhmadRonty Ahmad
Hi Mohit
Mainly in your <apex:pageBlockTable value="{!ContactList}" var="Con"> you are using Con with capital C but in <apex:column value="{!con.Name}" title="Contact Name"/> you are using lower con.
just change var="Con" to var="con".  your all ohter code is fine.
 
<apex:pageBlockTable value="{!ContactList}" var="Con">
<apex:column value="{!con.Name}" title="Contact Name"/>
</apex:pageBlockTable>

Thanks

Ronty
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Mohit,

Your code is working fine. I found only one issue. Just updated the Con to con

<apex:pageBlock >
<apex:pageBlockTable value="{!ContactList}" var="con">
<apex:column value="{!con.Name}" title="Contact Name"/>
</apex:pageBlockTable>
</apex:pageBlock>

NOTE:- You need to pass Account Record ID in URL like below screen shot

YOUR URL SHOULD BE LIKE BELOW

BASE_URL/apex/YOUR_PAGE_NAME?id=0016A000006WIuj

Pass record ID according to your org.
User-added image
 
mohit tripathimohit tripathi
Thanks Amit, Devanshu and Ronty for quick support.

This was so silly of me.
You all are champ.


Thanks and Cheers.