+ Start a Discussion
Sandra Rendel 7Sandra Rendel 7 

Show related custom objects in account details page

I have 2 custom objects:
 courses
 assign courses (connect courses to contacts)
I need to show the list of courses which are related to the contacts of one account in its details page for instacne as a related list
Amit Singh 1Amit Singh 1
Hi Sandra,

For this, you can create an Inline VF page using Account as Standard Controller. Create an Apex which will be used as Controller/Extensions and inside extensions you can write the logic for fetching the related courses and then can show into the VF page.

Add this VF page to Account Related Page.

Hope this will help :)

Thanks,
Amit Singh
Sandra Rendel 7Sandra Rendel 7
Hello
Thanks for your guide, do you have a sample apex code for this
I would appreciate if if you ould help me about this
 
Amit Singh 1Amit Singh 1
Hello Sandra,

I have developed some sample code for you that is given below:

Apex Class:
public class showRelatedList{

    public List<Contact> contactList {get; set;}
    public List<Opportunity> relatedOpportunity {get; set;}
    public Id accountId {get; set;}
    
    public showRelatedList(ApexPages.StandardController controller) {
        accountId = ApexPages.currentPage().getParameters().get('id');
        relatedOpportunity = new List<Opportunity>();
        System.debug('#### accountId = '+accountId);
        contactList = [Select Id, Name, (Select Id, Name, Amount, CloseDate From Opportunities__r) From Contact Where AccountId=:accountId];
        System.debug('#### contactList ='+contactList);
        if(contactList!=null && contactList.size()>0){
            //System.debug('#### con.Opportunities__r = '+con.Opportunities__r);
            For(Contact con : contactList){
                System.debug('#### con.Opportunities__r = '+con.Opportunities__r);
                If(con.Opportunities__r!=null && con.Opportunities__r.size()>0){
                    
                    relatedOpportunity.addAll(con.Opportunities__r);
                }
            }
        }
    }
    
}

VF page:
<apex:page standardController="Account" extensions="showRelatedList">
  <!-- {!relatedOpportunity}<br/><br/>
  {!contactList}
  {!contactList[0].Name} -->
  <apex:pageBlock title="Related Opportunities">
      <apex:pageBlockSection columns="4">
          
          <apex:pageBlockTable value="{!relatedOpportunity}" var="opp">
              <apex:column headerValue="OpportunityName">
                  <apex:outputField value="{!opp.Name}"/>
              </apex:column>
              <apex:column headerValue="Amount">
                  <apex:outputField value="{!opp.Amount}"/>
              </apex:column>
              <apex:column headerValue="Close Date">
                  <apex:outputField value="{!opp.CloseDate}"/>
              </apex:column>
          </apex:pageBlockTable>
      </apex:pageBlockSection>
  </apex:pageBlock>
</apex:page>

Result Image after adding Inline VF page.
Result Image

Note: - In the solution, I have created a New Lookup field on Opportunity related to Contact Object and then showing all related Opportunity that is related to any Contact of particular opportunity
 
Let us know if this helps :)
 
Thanks.,
Amit Singh
Sandra Rendel 7Sandra Rendel 7
Thanks for your help
Amit Singh 1Amit Singh 1
Sandra if this helps, mark as best answer
Sandra Rendel 7Sandra Rendel 7
I have also antoher question, I added the questoin for forum I need to show a report on home page, I added it on a VF but added VF on home page but when I click on homage page it redriecs me to the repots page