+ Start a Discussion
Dvisha TDvisha T 

Create visualforce page which displays Account and its related oppurtunities

Hi,

Thanks in advance,
Am new to apex and visualforce,kindly helpme in the below requirement,
I need to create a custom button on account object and on clicking on it should lead to a visualforce page that contain some of the account and related oppurtunities of the account.kinldy help me out by providing a sample code.

 
Best Answer chosen by Dvisha T
Dayakar.DDayakar.D
Hi Dvisha T,

create VF page and its Controller as below.


 
<apex:page standardController="Account" extensions="Account_andRelatedContacts" sidebar="false">
    <apex:form >
        <apex:pageBlock >
                <table border="1" >
                    <tr>
                        <th>Name</th>
                        <th>phone</th>
                        <th>Related Opportunities</th>
                    </tr>
                    
                    <tr> <apex:repeat value="{!accountRecords}" var="accountVar">
                        <tr>
                            <td>{!accountVar.name}</td>
                            <td>{!accountVar.phone}</td>
                        <td  >
                            <table border="1" style="width:100%">
                                <caption colspan="2">{!accountVar.name} Opportunities...</caption>
                                <tr>
                                    <th>Name</th> <th>Amount</th>
                                </tr>
                                <apex:repeat value="{!accountVar.Opportunities}" var="OppVar">
                                    <tr>
                                        <td>{!OppVar.name}</td>
                                        <td>{!OppVar.amount}</td>
                                    </tr>
                                </apex:repeat>
                            </table>
                        </td>
                        </tr>
                        </apex:repeat>
                    </tr>
                </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>



public class Account_andRelatedContacts {
    string id=ApexPages.CurrentPage().getparameters().get('id');
    public Account_andRelatedContacts(ApexPages.StandardController controller)
    {
        
    }
    
    public map<account,list<Opportunity>> accountContactMap=new map<account,list<Opportunity>>();

    public map<account,list<Opportunity>> accountRecords {
        set;
        get {
            list<account> listOfAccounts= [select id,name,phone,billingcity,(select id,name,amount from Opportunities) from account where id=:id limit 5];
          for(account singleAccount:listOfAccounts)
            {
                accountContactMap.put(singleAccount,singleAccount.Opportunities);
            }
            return accountContactMap;
        }
        
    }
}

Then create a button in Account (in quick find type account----click on Buttons, Links, and Actions--- click on New button or link  and fill in all details , in Content Source pick list select visualforce page  and  in Content pick list select above created vf page.--- Save).

Add Created button in account layout.

Please let me know, if it helps you.

Best Regards,
Dayakar.D

 

All Answers

Dayakar.DDayakar.D
Hi Dvisha T,

create VF page and its Controller as below.


 
<apex:page standardController="Account" extensions="Account_andRelatedContacts" sidebar="false">
    <apex:form >
        <apex:pageBlock >
                <table border="1" >
                    <tr>
                        <th>Name</th>
                        <th>phone</th>
                        <th>Related Opportunities</th>
                    </tr>
                    
                    <tr> <apex:repeat value="{!accountRecords}" var="accountVar">
                        <tr>
                            <td>{!accountVar.name}</td>
                            <td>{!accountVar.phone}</td>
                        <td  >
                            <table border="1" style="width:100%">
                                <caption colspan="2">{!accountVar.name} Opportunities...</caption>
                                <tr>
                                    <th>Name</th> <th>Amount</th>
                                </tr>
                                <apex:repeat value="{!accountVar.Opportunities}" var="OppVar">
                                    <tr>
                                        <td>{!OppVar.name}</td>
                                        <td>{!OppVar.amount}</td>
                                    </tr>
                                </apex:repeat>
                            </table>
                        </td>
                        </tr>
                        </apex:repeat>
                    </tr>
                </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>



public class Account_andRelatedContacts {
    string id=ApexPages.CurrentPage().getparameters().get('id');
    public Account_andRelatedContacts(ApexPages.StandardController controller)
    {
        
    }
    
    public map<account,list<Opportunity>> accountContactMap=new map<account,list<Opportunity>>();

    public map<account,list<Opportunity>> accountRecords {
        set;
        get {
            list<account> listOfAccounts= [select id,name,phone,billingcity,(select id,name,amount from Opportunities) from account where id=:id limit 5];
          for(account singleAccount:listOfAccounts)
            {
                accountContactMap.put(singleAccount,singleAccount.Opportunities);
            }
            return accountContactMap;
        }
        
    }
}

Then create a button in Account (in quick find type account----click on Buttons, Links, and Actions--- click on New button or link  and fill in all details , in Content Source pick list select visualforce page  and  in Content pick list select above created vf page.--- Save).

Add Created button in account layout.

Please let me know, if it helps you.

Best Regards,
Dayakar.D

 
This was selected as the best answer
Dvisha TDvisha T
Hi Dayakar,

Thank you for your great help it worked,hope yoyu can help me on another requirement.
Continue from above scenraio,
Clicking on the Opportunity name in Vf page it should expand and display the product details with expand and collapse option
and also a button on the top as Contacts willl open as a pop up with list of contact information.
 
Dvisha TDvisha T
Can we do the expand and collapse without using Javascrip or Jquerry?