function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sreeram ChakrapaniSreeram Chakrapani 

Display Records from a Custom Object tied to a Opportunity

Hi Folks,

I have a custom object that is related to the opportunity object through a master detail relationship. On the click of a button which is on a Visual Force Page, i would like to open up another Visual Force Page and display all the records from the custom object related to the opportunity, can somebody help me out with starter code for this ? Appreciate the help

Thanks
Sree
Best Answer chosen by Sreeram Chakrapani
James LoghryJames Loghry
On your first VF page, you would have a link to open the new Visualforce page.  Similar to the below (although this may need a bit of work):

<apex:commandLink value="Related Records for {!opp.Name}" onclick="window.open({!$Page.SecondPage}?id=opp.Id);" />

Then your second page would use the Opportunity as a Standard Controller, along with an apex class for your Extension.  For more on Standard Controllers and Extensions, see here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

I
n general, your second page, which displays the related values, would look like:

<apex:page standardController="Opportunity" extensions="OpportunityChildExt">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!oppChildren}" var="child">
                <apex:column value="{!child.Name}" />
                <apex:column value="{!child.CustomField__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Your apex extension class might look like the following:

public with sharing class OpportunityChildExt{

    public List<CustomObject__c> opportunityChildren {get; set;}

    //Default constructor for extension classes
    public OpportunityChildExt(ApexPages.StandardController ctrl){
        Opportunity opp = (Opportunity)ctrl.getRecord();
        opportunityChildren = 
            [Select 
                 Id
                ,OtherFieldsGoHere 
             From 
                 CustomObject__c 
             Where 
                 Opportunity__c = :opp.Id];
    }
}


All Answers

James LoghryJames Loghry
On your first VF page, you would have a link to open the new Visualforce page.  Similar to the below (although this may need a bit of work):

<apex:commandLink value="Related Records for {!opp.Name}" onclick="window.open({!$Page.SecondPage}?id=opp.Id);" />

Then your second page would use the Opportunity as a Standard Controller, along with an apex class for your Extension.  For more on Standard Controllers and Extensions, see here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

I
n general, your second page, which displays the related values, would look like:

<apex:page standardController="Opportunity" extensions="OpportunityChildExt">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!oppChildren}" var="child">
                <apex:column value="{!child.Name}" />
                <apex:column value="{!child.CustomField__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Your apex extension class might look like the following:

public with sharing class OpportunityChildExt{

    public List<CustomObject__c> opportunityChildren {get; set;}

    //Default constructor for extension classes
    public OpportunityChildExt(ApexPages.StandardController ctrl){
        Opportunity opp = (Opportunity)ctrl.getRecord();
        opportunityChildren = 
            [Select 
                 Id
                ,OtherFieldsGoHere 
             From 
                 CustomObject__c 
             Where 
                 Opportunity__c = :opp.Id];
    }
}


This was selected as the best answer
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Chakrapani,

Use the following code for your requirement.

First Page:
***********
<apex:page controller="SampleController">
<apex:form >
    <apex:commandButton value="redirectPage"/>
</apex:form>
</apex:page>

First Controller:
*****************

public class SampleController {

    public Opportunity opp = new Opportunity();

    public SampleController(){
   
        opp = [SELECT Id FROM Opportunity Limit 1];//include your logic to select the opportunity record.
    }

    public pagereference redirectPage(){
   
        pagereference ref = new pagereference('/apex/NewPage?id='+opp.Id);
        ref.setRedirect(true);
        return ref;
    }
}

Second Page:
*******************

<apex:page controller="Newcontroller123">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!cLst}" var="c">
            <apex:column value="{!o.id}"/>
            <apex:column value="{!o.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form> 
</apex:page>

Second Controller:
**********************

public class Newcontroller123 {

    public Opportunity[] cLst { get; set; }
   
    public Newcontroller123(){
   
        cLst = new Opportunity[]{};
        cLst = [SELECT Id,name from Custom_Object__c WHERE Opportunity__c =: apexpages.currentpage().getparameters().get('id')];
       //Opportunity__c is the reference field for the Opportunity obj  on the Custom Object.
    }
}

Dont forget to like

Thanks,
Vijay
Sreeram ChakrapaniSreeram Chakrapani
Thank you both for the replies, this is exactly what I was looking for