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
Matt_FMatt_F 

Link Opportunity object and custom object on VF page

Here is what I am hoping to accomplish:

 

 I have a custom object "Value Test" that is linked to the Opportunity object.  I am creating a VF form to populate the Value Test object.  Some of the fields on the VF form will take information from the opportunity, so I would like those to autofill.  I also want to create a custom button on the opportunity to open the VF form.  The problem I am having is trying to figure out how to be able to access the fields for both the Value Test custom object and the Opportunity object on the same VF form.  If I use Value_Test__c as the controller, I can't pull any information from the opportunity object into the form and I cannot add the custom button to the opportunity.  If I use Opportunity as the controller, I can't populate any information from the VF form to the Value Test custom object.  I am pretty sure that I need to use a controller extension, but I'm not familiar with them. 

 

Any help that you can give me will be greatly appreciated.

 

Thanks,

 

Matt

Michael_TorchedloMichael_Torchedlo

Matt,

 

In my company we did something similar - a custom object which is a child of the Opportunity.  And we used a custom VF page when making a new record, so that some of the fields would auto-populate.  I used an apex controller extension.  You should check the Visualforce Developers guide for more information on Controller Extensions.

 

On your custom object Value Test, you can override the standard "New" button with your visualforce page.  Since it is a child of the opportunity, that means when the user clicks the "New Value Test" button on the opportunity page layout, it is already passing in the OpportunityId to the new record.  So all you need to do is perform a query in your apex constructor method  on the related object (Opportunity) and get any other field values you need.  Then, you assign those  values into the fields on your new object, and when the VF page first renders, those are the values that are auto-populated.  Because I used an extension and not a full controller, the standard Save operation works just like a regular page, so you can add a save command button to your VF page.

  

public with sharing class ValueTestControllerExtension{

    public ValueTestControllerExtension(ApexPages.StandardController stdController){
        //constructor initializes the page

 

        //first, you get your new Custom Object from the standard controller getRecord() method

        Value_Test__c MyCustObj = (Value_Test__c)stdController.getRecord();     

       

        //you can do a single query to get any values you may need from the parent object

        //Opportunity__c is the field name of the parent relationship

        if(MyCustObj.Opportunity__c!=NULL){
            Opportunity O = [SELECT id,ownerid,name,accountid,amount FROM Opportunity

                   WHERE id =:MyCustObj.Opportunity__c LIMIT 1];

 

            MyCustObj.name = O.name+' -new object name ';

            MyCustObj.field_name2__c = O.amount;

            // ... etc.
        }
    }

}


 

 

<apex:page standardController="Value_Test__c" extensions="ValueTestControllerExtension" >

  ...

</apex:page>