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
dany__dany__ 

how to get the account id of contact

here is my code (i just dont want to give hard code id ) . plz guide me
----------------------------------------
<apex:page standardController="contact" extensions="pg">
 <apex:form >
<apex:inputField value="{!contact.accountid}"/>

 </apex:form>
 <apex:form >
 <apex:pageBlock title="contact details">
  <apex:pageBlockSection columns="2">
  <apex:pageBlockTable value="{!cont}" var="co">
  <apex:column value="{!co.lastname}"/>
    <apex:column value="{!co.phone}"/>
      <apex:column value="{!co.email}"/>
  </apex:pageBlockTable>
  </apex:pageBlockSection>
  </apex:pageBlock>
  <apex:pageBlock title="oppty details">
  <apex:pageBlockSection >
  <apex:pageBlockTable value="{!oppty}" var="op">
  <apex:column value="{!op.name}"/>
    <apex:column value="{!op.Amount}"/>
  </apex:pageBlockTable>
  </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>
--------------------
public class pg {

    public pg(ApexPages.StandardController controller) {
     cont=[select id, firstname, lastname , email,phone from contact where accountid =:'cont.accountid']; 
      oppty=[select name, amount from opportunity where accountid='0019000001CuKu3'];
    

    }


    

    public list<contact> cont { get; set; }
        public list<opportunity> oppty{ get; set; }
    
   public pg()
    {
     
    
    }
}
Shaijan ThomasShaijan Thomas
not sure what you are looking
cont=[select id, firstname, lastname , email,phone from contact];
oppty=[select name, amount from opportunity where accountid=:cont.AccountId];
is the somthing like this?
Vijay NagarathinamVijay Nagarathinam
Try this,

public pg(ApexPages.StandardController controller)
{
      cont=[select id, firstname, lastname , email,phone from contact where accountid ='0019000001CuKu3']; 
      oppty=[select name, amount from opportunity where accountid='0019000001CuKu3'];
 }
 
dany__dany__
There is  a look up of account in my vf oage when ever i select a account then the details of that particular contact must appear 
ManjunathManjunath

Hi Dany,

If I have understood it properly, you have created a lookup field which on selecting the account it should display the contacts and opportunity.

In that case I have changed it a little in the code. Check if it helps
 
public contact contactRec {get;set;}
    public pg() {
        contactRec = new contact();
        cont= new list<contact>();
    }
    public list<contact> cont { 
    get {
        cont=[select id, firstname, lastname , email,phone,accountid from contact where accountid =:contactRec.accountid];
        return cont;
    } 
    set; 
    }
    public list<opportunity> oppty{ 
        get{
            oppty=[select name, amount from opportunity where accountid=:contactRec.accountid];
            return oppty;
        } 
        set; 
    }
 
<apex:page controller="pg">
    <apex:form >
        <apex:inputField value="{!contactRec.accountid}" >
            <apex:actionSupport event="onchange" reRender="form" status="progress"/>
        </apex:inputField>
        <apex:actionStatus id="progress"  >
            <apex:facet name="start" >
                <img src="/img/loading.gif" alt="Loading graphic" />
            </apex:facet>
        </apex:actionStatus>
    </apex:form>
    <apex:form id="form">
        <apex:pageBlock title="contact details" >
            <apex:pageBlockSection columns="2">
                <apex:pageBlockTable value="{!cont}" var="co">
                    <apex:column value="{!co.lastname}"/>
                    <apex:column value="{!co.phone}"/>
                    <apex:column value="{!co.email}"/>
                    <apex:column value="{!co.accountid}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="oppty details">
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!oppty}" var="op">
                    <apex:column value="{!op.name}"/>
                    <apex:column value="{!op.Amount}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



Regards,
Manjunath
dany__dany__
@manjunath
thnx for your response . can we do the same using standard controller . if yes ? plz guide me 
ManjunathManjunath
Dany,

Is there a specific reason that you want to go with the standard controller?

Check if this helps!!
<apex:page standardController="Contact" extensions="ApexTutorial_dynamicObjectCreation">
    <apex:form >
        <apex:inputField value="{!contactRec.accountid}" >
            <apex:actionSupport event="onchange" reRender="form" status="progress"/>
        </apex:inputField>
        <apex:actionStatus id="progress"  >
            <apex:facet name="start" >
                <img src="/img/loading.gif" alt="Loading graphic" />
            </apex:facet>
        </apex:actionStatus>
    </apex:form>
    <apex:form id="form">
        <apex:pageBlock title="contact details" >
            <apex:pageBlockSection columns="2">
                <apex:pageBlockTable value="{!cont}" var="co">
                    <apex:column value="{!co.lastname}"/>
                    <apex:column value="{!co.phone}"/>
                    <apex:column value="{!co.email}"/>
                    <apex:column value="{!co.accountid}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="oppty details">
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!oppty}" var="op">
                    <apex:column value="{!op.name}"/>
                    <apex:column value="{!op.Amount}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public contact contactRec {get;set;}
    public ApexTutorial_dynamicObjectCreation(ApexPages.StandardController controller) {
        contactRec = new contact();
        cont= new list<contact>();
    }
    public list<contact> cont { 
    get {
        cont=[select id, firstname, lastname , email,phone,accountid from contact where accountid =:contactRec.accountid];
        return cont;
    } 
    set; 
    }
    public list<opportunity> oppty{ 
        get{
            oppty=[select name, amount from opportunity where accountid=:contactRec.accountid];
            return oppty;
        } 
        set; 
    }

Regards,
Manjunath

 
ManjunathManjunath
Dany,

By the way, as a best practice we shoud not be quering in getter/setter (Gave you as an example.) . Change accrodingly.

Regards,
Manjunath