I have a custom object that has a Master-Detail relationship to the Opportunity object. I created a Visualforce page that I want to use to override the standard "New" list button on the related list of the custom object on the Opportunity. However, I want to be able to pass through the Opportunity ID and the Owner ID to the newly created page, as there are some variables that are formula lookups to fields on the Opportunity and Account. Does anyone know how I can do this? In the past I've used a URL hack to create the button, but I understand those will be going away soon, so I would like a more scalable way to do this. Thanks!
VF Page:
<apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="VF_SFController" action="{!SF}">
<apex:form >
<apex:repeat value="{!ESF}" var="SF">
Contact Information
<Table columns="5" width="100%">
<tr>
<td width="10%">Name:</td>
<td width="38%"><apex:outputField value="{!SF.Rep__c}"/></td>
<td width="4%"> </td>
<td width="10%">Amount:</td>
<td width="38%"><apex:outputField value="{!SF.Amount__c}"/></td><p class="double"></p>
</tr>
<tr>
<td width="10%">Phone:</td>
<td width="38%"><apex:outputField value="{!SF.Sales_Rep_Phone__c}"/></td>
<td width="4%"> </td>
<td width="10%">Opportunity:</td>
<td width="38%"><apex:outputField value="{!SF.Opportunity__c}"/></td><p class="double"></p>
</tr>
<tr>
<td width="10%">Email:</td>
<td width="38%"><apex:outputField value="{!SF.Sales_Rep_Email__c}"/></td>
<td width="4%"> </td>
<td width="10%">Account:</td>
<td width="38%"><apex:outputField value="{!SF.Legal_Name__c}"/></td><p class="double"></p>
</tr>
</Table>
</apex:repeat>
</apex:form>
</apex:page>
Custom Controller:
public class VF_SFController{
public List<SF__c> ESF {get; set;}
private final Opportunity opp;
public VF_SFController(ApexPages.StandardController myController){
SF = new List<SF__c>();
opp=(Opportunity)myController.getrecord();
}
public SF__c SF2 = new SF__c();
public void SF(){
SF2.Opportunity__c = opp.Id;
SF2.Rep__c = opp.OwnerId;
SF2.Amount__c = opp.Amount;
SF.add(SF2);
}
public PageReference save() {
PageReference RetPage = new PageReference('/apex/SFViewPage?id=' + SF[0].id);
RetPage.setRedirect(true);
return RetPage;
}
}
getRecord()
will include the Opportunity Id plus any merge fields on the VF page that are bound to the Opportunity object. If you don't include these, then you need to query for them in either the constructor or, through use of a page action method (that gets called after the constructor finishes)getRecord()
works like trigger contexts and the entire Opportunity record is available to the controller. Not true.getRecord()
only makes available to the controller the fields defined on the page of the form{!Opportunity.xxx}
- these can be hidden fields -- or, you can use methodaddFields()
. See doc on each method here: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…