Sunday, December 2, 2012

How to add a VisualForce page as Related list in Salesforce

My objective is add a related list in any object record detail page.
For example: I want to add Opportunity list in Product detail page. i.e. Each Product record detail page will display its related Opportunity as well.

How can I accomplish it?
- Develop a visualforce page containing a page block table which displays list of related opportunities.
- Open Product layout. Add the VF page in a new section at detail lay out.


Here is the code for related List of Opportunity:
VF page:









   
  
       
       
       
           
       
       
       
        
        
          
       
    
   
 
 



Controller:



public with sharing class CustomRelatedListForProductController {
 
 //Variable declared Globally to hold record of Product and OppLineItem Object
    public Product2 productObj{get;set;}
    public List listofOppLineItem;
    
    /**
    *@purpose : Constructor to fetch current Product Object Record with Standard Controller as Parameter
    **/
 
 public CustomRelatedListForProductController(ApexPages.StandardController controller) {
        this.productObj = (Product2)controller.getRecord();
        listofOppLineItem = new List();
    }
    
    public List getListofOppLineItem(){
     listofOppLineItem = [SELECT id, UnitPrice, OpportunityId,
           PricebookEntry.Product2Id, Opportunity.Account.name,
                 TotalPrice, Quantity, Description, Opportunity.name,
                 Opportunity.CloseDate, ListPrice
              FROM OpportunityLineItem 
              WHERE PricebookEntry.Product2Id =: productObj.ID];
  return listofOppLineItem;
    }
}

No comments:

Post a Comment