You need to sign in to do that
Don't have an account?
NewBee21
Lookup field for Account and getting all the contacts related to that Account using extension controller
I am getting this error while previewing visualforce page. ==> Error: Unknown property 'AccountStandardController.con'
Visualforce page:
<apex:page standardController="Account" extensions="ExtensionController5">
<apex:form >
<apex:pageBlock title="Account Information" id="account"> <apex:pageBlocksection>
<apex:inputField value="{!con.accountId}"/>
<apex:actionsupport event="onchange" action="{!accountselected}" rerender="account,msgs" status="stat"/>
</apex:pageBlockSection
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ExtensionController5{ private ApexPages.StandardController Stdctrl;
public ExtensionController5(ApexPages.StandardController controller){ Stdctrl=controller; }
public void AccountSelected() { contact con=(contact) StdCtrl.getRecord();
if (!string.IsBlank(con.accountid))
{ con.account=[select Name,Phone from Account where id=:con.accountid]; }
else { con.account=null; } } }
Visualforce page:
<apex:page standardController="Account" extensions="ExtensionController5">
<apex:form >
<apex:pageBlock title="Account Information" id="account"> <apex:pageBlocksection>
<apex:inputField value="{!con.accountId}"/>
<apex:actionsupport event="onchange" action="{!accountselected}" rerender="account,msgs" status="stat"/>
</apex:pageBlockSection
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class ExtensionController5{ private ApexPages.StandardController Stdctrl;
public ExtensionController5(ApexPages.StandardController controller){ Stdctrl=controller; }
public void AccountSelected() { contact con=(contact) StdCtrl.getRecord();
if (!string.IsBlank(con.accountid))
{ con.account=[select Name,Phone from Account where id=:con.accountid]; }
else { con.account=null; } } }
Please consider the code below, this will give you output as per your requirement -
Output -
Enter Account name in search box then click on show Contacts button, then related contacts will be displayed.
*****SCREENSHOT******
Thank you!
All Answers
Please consider given code below -
Please mark it as the best answer if it helps.
Thank you!
My requirement is I want the contacts of the Account which I search with the look up (see screenshot) and then I will get contacts related to the account to me.
heres what i achieved till the lookup part,but now stuck with how do i get contacts fetched to me of the account.
**controller**
public class freshcontroller1
{
public contact con {get;set;}
Account ac;
public freshcontroller1(ApexPages.Standardcontroller controller)
{
con =new contact();
}
public Account getAc()
{
return ac;
}
public pagereference showcontact()
{
ac = [select id from Account where id=:con.accountid];
System.debug('Ac '+ac);
return null;
}
public pagereference Contact()
{
con=[select id,Name,Phone from Contact where id=:con.accountid];
System.debug('contacts are '+con);
return null;
}
}
*VF Page**
<apex:page standardController="Account" extensions="freshcontroller1">
<apex:form > <apex:pageBlock title="Account information"/>
<apex:inputField value="{!con.accountid}"/>
<apex:commandButton action="{!showcontact}" value="Display contacts"/>
<apex:pageBlock > <apex:pageBlockTable value="{!con}" var="c"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Please consider the code below, this will give you output as per your requirement -
Output -
Enter Account name in search box then click on show Contacts button, then related contacts will be displayed.
*****SCREENSHOT******
Thank you!
But I want to search for account using search option (please refer screenshot) and once I select Account,then I want contacts to be shown.
You correctly solved my query about contacts fetching parts,no doubt.But I just want,Account to be searched via search option instead manually entering Account.
screenshot: