+ Start a Discussion
rajesh k 10rajesh k 10 

How to display contact related Account and Opportunity based on selected contact?

How to display list of contacts, when I select one contact, it shoud display related account and Opportunity?

please help me......
asish1989asish1989
Hi Rajesh,

As we know contact is the Child of Accouunt, and contact is the Parent for Opportunity. So one contact can be associated to only one Account and one contact may have list of Opportnity. 

I guess you must be selecting contact from a options list in a vf page, when you select please store contact id in a variabe and in the controller method which will be called on the onchnage event. Now make two query, one querry for Account one querry for Opportunity. 

here is some code snippet,

public List<SelectOptions> options{get;set;}
public String Accountid{get;set;}
public list<Opportunity> listOfOppt{get;set;}
Public Account account {get;set;}
options = new List<SelectOptions>();

for(Contact c:[select id,Name from Contcat]){
    options.add(c.id, c.name);
}

public PageReference methdcalledOnchange(){
   
     account = [select id ,name from Account where id=:Accountid];    
     listOfOppt = [select id,name form Opportinity where Accountid = :Accountid]
    return null;
}

<apex:page>


<apex:selectList value ="{!Accountid}" size="1">
   <apex:selectOptions value = "{!options}"/>
        <apex:actionsupport event ="Onchange" action="{!methdcalledOnchange}" rereder="accpountblock, opptblock"/>
</apex:seelctList>

//account block
<apex:outputField value ="{!account.name}"/>

//oppt block
<apex:pageBlocktable value = "{!listOfOppt}" var="oppt">
    <apex:column value="{!oppt.Name}"/>
    //rest column
</apex:pageBlockTable>

mark it solved if it helps you so that others get benifit.

Thanks
Asish
Do Visit this blog for Salesforce refrence -- http://salesforceworld4u.blogspot.in/



NaveenReddyNaveenReddy
Hi Rajesh,

 Here is the code you requested.Please let me know if anything is missing.

<apex:page controller="ContactPicklistCont">
	<apex:form>
		<apex:pageBlock title="Contact Detail">
			<apex:pageblockSection>
				<apex:pageblockSectionItem>
					<b>Contacts:</b>
					<br />
					<apex:selectList value="{!selectedCon}" size="1">
						<apex:selectOptions value="{!Contacts}"></apex:selectOptions>
						<apex:actionSupport action="{!opportunityList}" event="onchange" />
					</apex:selectList>
				</apex:pageblockSectionItem>
				<apex:pageblockSectionItem>
					<b>Opportunities:</b>
					<br />
					<apex:selectList value="{!selectedOpp}" size="1">
						<apex:selectOptions value="{!oppList}"></apex:selectOptions>
					</apex:selectList>
				</apex:pageblockSectionItem>
				<apex:pageBlockSectionItem>
					<b>Account Name:</b>
					<br />
					<apex:outputText value="{!selectedAcc}"></apex:outputText>
					</apex:pageblockSectionItem>
			</apex:pageblockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
public class ContactPicklistCont {

	Public String selectedOpp{get;set;}
	Public String selectedAcc{get;set;}
	Public String selectedCon{get;set;}
	public List<SelectOption> oppList{get;set;}


	public List<SelectOption> getContacts() {
		List<SelectOption> conOptions = new List<SelectOption>();
		conOptions.add( new SelectOption('','--Select--'));
		for( Contact con: [select Id,name from Contact] ) {
			conOptions.add( new SelectOption(con.Id,con.name));
		}
		return conOptions;
	}
	public void opportunityList() {

		Contact con = [SELECT Contact.Account.Name FROM  Contact  WHERE Id =:selectedCon];
		selectedAcc = con.Account.Name;
		oppList = new List<SelectOption>();
		oppList.add( new SelectOption('','--Select--'));
		if( selectedCon != null && selectedCon.length() > 0 ) {
			for( Opportunity opp: [SELECT Id , name FROM   Opportunity WHERE  AccountId IN (SELECT AccountId from Contact WHERE Id =:selectedCon)]) {
				oppList.add( new SelectOption(opp.Id,opp.name));
			}
		}
	}

}


User-added image

***Please mark as best answer if it solves your problem.

Regards,
Naveen
SSE , Salesforce CI expert group
http://www.autorabit.com
Automated deployments , Dataloader , Sandbox back-ups, test automation for Salesforce applications

rajesh k 10rajesh k 10
Hi NaveenReddy,
                              I displayed hierarchy of Account,Opportunity and contacts records.But  Based on picklist values(Dates) how to display Account,Opportunity and contacts Hierarchy related records.See below code:

My page:
---------
<apex:page controller="Ctrl_MonthYearpicklist">
  <apex:form >
  <apex:pageBlock >
  <apex:selectList size="1" value="{!myDateRange}">
     <apex:selectOptions value="{!DateRangeOptions}"/>
     <apex:actionSupport event="onchange" reRender="out1"/>
</apex:selectList>
<apex:commandButton value="Go" action="{!doAction}" />

<apex:pageBlockSection rendered="{!isBoolean}">

          <ul>
        <apex:outputLabel value="Account:">
        <apex:repeat value="{!accounts}" var="account">
      
            <li>
                {!account.Name}
              
                <ul>
                <apex:outputLabel value="Opportunity:">
                <apex:repeat value="{!account.children}" var="opportunity">
                    <li>
                        {!opportunity.Name}
                       
                        <ul>
                        <apex:outputLabel value="Contact:">
                        <apex:repeat value="{!opportunity.children}" var="contact">
                            <li>
                                {!contact.Name}
                            </li>
                        </apex:repeat>
                        </apex:outputLabel>
                        </ul>
                       
                    </li>
                </apex:repeat>
                </apex:outputLabel>
                </ul>
               
            </li>
        </apex:repeat>
        </apex:outputLabel>
        </ul>
      

</apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Controller:
------------


public class Ctrl_MonthYearpicklist {

 

    public Boolean isBoolean { get; set; }

 

 

    public String myDateRange { get; set; }
  
    public List<SelectOption> getDateRangeOptions() {

        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('1-2014','Jan-2014'));
        options.add(new SelectOption('2-2014','Feb-2014'));
        options.add(new SelectOption('3-2014','Mar-2014'));
        options.add(new SelectOption('4-2014','Apr-2014'));
        options.add(new SelectOption('5-2014','May-2014'));
        options.add(new SelectOption('6-2014','june-2014'));
        options.add(new SelectOption('7-2014','july-2014'));
        options.add(new SelectOption('8-2014','Aug-2014'));
        options.add(new SelectOption('9-2014','Sep-2014'));
        options.add(new SelectOption('10-2014','Oct-2014'));
              
        return options;
    }
     public List<Wrapper> accounts{
        get
        {
        
        Date onOrAfter=Date.newInstance(01, 01, 2014);
            List<Wrapper> accounts = new List<Wrapper>();
            List<Id> oppIds = new List<Id>();
            for(Account acc : [Select Id, Name, (Select Id, Name From Opportunities) From Account])
            {
                Wrapper account = new Wrapper();
                account.Id = acc.Id;
                account.Name = acc.Name;
                account.children = new List<Wrapper>();
                accounts.add(account);
                for(Opportunity opp : acc.Opportunities)
                {
                    Wrapper opportunity = new Wrapper();
                    opportunity.Id = opp.Id;
                    opportunity.Name = opp.Name;
                    opportunity.children = new List<Wrapper>();
                    account.children.add(opportunity);
                    oppIds.add(opp.Id);
                }
            }
            Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, (Select Id, Contact.Name From OpportunityContactRoles) From Opportunity where id In :oppIds]);
            for(Wrapper account : accounts)
            {
                for(Wrapper opportunity : account.children)
                {
                    for(OpportunityContactRole opCR : oppMap.get(opportunity.Id).OpportunityContactRoles)
                    {
                        Wrapper contact = new Wrapper();
                        contact.Id = opCR.Id;
                        contact.Name = opCr.Contact.Name;
                        opportunity.children.add(contact);
                    }
                }
            }
            return accounts;
        }
    }
  
  
     public PageReference doAction() {
     isBoolean =true;
        return null;
    }
  
     public class Wrapper{
        public Id Id{get;set;}
        public String Name{get;set;}
        public List<Wrapper> children{get;set;}
    }

}

please help me...............