[SalesForce] Getting data of related record in visualforce page

i have one vf page and used two object Account and Contact

page code :

<apex:page standardController="Contact">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Create Contact">
            <apex:inputField value="{!Contact.FirstName}"/>
            <apex:inputField value="{!Contact.lastName}"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection title="Select Account">
            <apex:inputField value="{!Contact.AccountId}"/>
            <apex:outputField value="{!Contact.Account.AccountNumber}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

problem is How can we dispaly AccountNumber of selected account ?

for eg. Account ABC is already exist with 123 AccountNumber

so, once we select ABC from account lookup field then that AccountNumber field will auto populate with 123 value in the same screen

Best Answer

I don't think you will be able to do something like that totally "off the shelf" with Visualforce. You will need a little Apex extension to load the Account details once they have been picked.

This snippet works for me -

Page:

<apex:page standardController="Contact" extensions="accIdext">
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection title="Create Contact">
        <apex:inputField value="{!Contact.FirstName}"/>
        <apex:inputField value="{!Contact.lastName}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Select Account">
    <apex:actionRegion>
        <apex:outputLabel>Account Name</apex:outputLabel>
        <apex:inputField value="{!Contact.AccountId}">
            <apex:actionSupport event="onchange" rerender="accNo" action="{!retrieveAccName}" immediate="false"/>
        </apex:inputField>
        <apex:outputPanel id="accNo">
            <apex:outputLabel>Account Number</apex:outputLabel>
            <apex:outputText value="{!accNum}"/>
        </apex:outputPanel>
    </apex:actionRegion>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Class:

public with sharing class accIdext {

public String accNum { get;set; }
public Contact cont { get;set; }

public accIdext(ApexPages.StandardController controller) {
    this.cont = (Contact)controller.getRecord();
}

public void retrieveAccName() {
    this.accNum = [SELECT AccountNumber FROM Account WHERE Id = :cont.AccountId].AccountNumber;
}
}

Now, there are a whole bunch of "limitations" and "assumptions" in here.. for example I know the apex:outputLabel field look different, but that can be fixed, and also I used the actionregion to overcome "required" field submission stuff in the Contact record, which you might not want happening, I don't know! You could populate the AccountNumber more directly into the Contact object rather than having a separate instance variable for it - but I don't think this is the time for discussing in-depth best practices for Apex...

You will also need to write test coverage for that tiny method!

Related Topic