+ Start a Discussion
k sfdck sfdc 

how to display account contact and opportunity records as hierarchy using apex in salesforce?

Hi,
          how to display account contact and opportunity records as hierarchy using apex in salesforce like below

Account
     ----> Opportunity
            ----- >Contact
akramakram
User this code,

VF,
<apex:page controller="accountListController">
    <apex:pageblock >
        <ul>
        <apex:repeat value="{!accounts}" var="account">
            <li>
                {!account.Name}
                <ul>
                <apex:repeat value="{!account.children}" var="opportunity">
                    <li>
                        {!opportunity.Name}
                        <ul>
                        <apex:repeat value="{!opportunity.children}" var="contact">
                            <li>
                                {!contact.Name}
                            </li>
                        </apex:repeat>
                        </ul>
                    </li>
                </apex:repeat>
                </ul>
            </li>
        </apex:repeat>
        </ul>
    </apex:pageblock>
</apex:page>

Controller,
public class accountListController
{
    public class Wrapper{
        public Id Id{get;set;}
        public String Name{get;set;}
        public List<Wrapper> children{get;set;}
    }
    public List<Wrapper> accounts{
        get
        {
            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;
        }
    }
}



k sfdck sfdc
Hi,
        Using PageBlockTable How to Display Above Account,Opportunity and Contacts between Below Dates

Hi,
               I want between these dates relate opportuniti and contacts and account records How?

Date onOrAfter=Date.newInstance(01, 01, 2014);
   
     Date onOrBefore=Date.newInstance(29, 09,2014);

please help me,,,,,,,,
Subramani_SFDCSubramani_SFDC
use soql like this 
SELECT Id
FROM Case 
WHERE  CreatedDate >= 2013-12-21T00:00:00Z  AND  CreatedDate <= 2013-12-23T23:59:59Z
k sfdck sfdc
Hi,
           How to display contacts hierarchy only opportunity and Account hierarchy only u displayed?

please help me....
akramakram
Hi

Can you please elaborate your requirement with some example? 
k sfdck sfdc
Hi akram,
                              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...............