Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Hi, 

I am trying to display the account record details as well as the related contacts and opportunities for a selected account record through lookup.

I am stuck at the point where I am unable to obtain the record id for the selected account record.

Below is my code. Any help is appreciated. 

VF Page:

<apex:page controller="accountdisplay">

    <apex:form>

        <apex:pageBlock title="Display Account Details">

            <apex:outputText value="Select Account: " />

            <apex:inputField value="{!con.accountid}"/>

            <apex:commandButton action = "{!getdetails}" value='Submit' />

            <apex:pageBlockSection title="Account Details">

                <apex:outputtext value="{!acc.name}" />

            </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form>

</apex:page>

Controller:

public class AccountDisplay {

    public account acc {get; set;}

    public contact con {get; set;}

        

    public pagereference getdetails(){

        acc = [select id, name from account where id = :con.AccountId]; 

        return null;

    }

}

Thank You,

Satya
5 answers
  1. Jul 8, 2015, 11:46 AM
    Hi,

    Please do the changes in your class  as mention below

     

    public class AccountDisplay {

    public account acc {get; set;}

    public contact con {get; set;}

    public AccountDisplay (){

    con=new contact();

    acc=new Account();

    }

    public pagereference getdetails(){

    system.debug('ccc=='+con);

    acc = [select id, name from account where id = :con.AccountId];

    return null;

    }

    }

    Thanks,

    Keyur Modi

     
  2. Jul 8, 2015, 3:10 PM

    Hi,

    public class AccountDisplay {

    public account acc {get; set;}

    public contact con {get; set;}

    public AccountDisplay (){ // here I created constructor for this class

    con=new contact(); // I create the instance of contact

    acc=new Account(); // I create the instance of Account

    }

    public pagereference getdetails(){

    system.debug('ccc=='+con); // This is to just check either value in con.account.id is comming or not so that account query will work properly

    acc = [select id, name from account where id = :con.AccountId];

    return null;

    }

    }

    Only one diffrance between my code and your code is you had not created the instance of the two object contact and account ... and so i create the instace of both oject in constructor so whenever your page load it will create instace for both object. so it will not give the error which you faced before.

    i hope this clarification will make your doubt clear .. if still you have any confusion feel free to ask,

    Thanks,

    Keyur Modi
  3. Jul 8, 2015, 2:39 PM
    Hi,

    Thanks for your reply. It worked. However, can you please explain your code especially the system.debug part.

    Thank you,

    Satya
0/9000