I Have a custom object called Withdrawal which is having a lookup to Opportunity object My requirement is to override the standard New creation button for the Withdrawal object and whenever someone selects a opportunity via the lookup in the Withdrawal object it should autopopulate the rest of the fields. For example: I have a student_id__c field in my opportunity as well as withdrawal object both Now at the time of record creation when I am selecting an opportunity from the lookup which has a student Id already present, it should automatically populate the student id of that opportunity in the withdrwrawal new record creation form. I am struggling hard to create something using VF page,but the prepopulation of fields is not coming up on display,please also suggest if the same can be done in lightning as well would be most helpful

<apex:page standardController="Withdrawal__c" extensions="WithdrawalExtension">
<apex:form id="theFrm">
<apex:inputField id="Opportunity" value="{!Withdrawal__c.Opportunity_Contact__c}">
<apex:actionSupport event="onchange" action="{!OppSelected}" rerender="theFrm"/>
<apex:inputText id="studentId" value="{!Withdrawal__c.Opportunity_Contact__r.Student_ID__c}"  />

public class WithdrawalExtension {
 private ApexPages.StandardController internalCon;

public WithdrawalExtension(ApexPages.StandardController con){
    internalCon = con;

public void OppSelected(){
    Withdrawal__c sa = (Withdrawal__c)internalCon.getRecord();
    Opportunity Opp = [Select Id, Student_ID__c From Opportunity where Id = :sa.Opportunity_Contact__c];

    sa.Opportunity_Contact__c = Opp.Id;



In a nutshell if I say it with an image

1)enter image description here

2)The student Id associated with that opportunity must be showing up in the same form itself auto-populated and auto-displayed in the same form.similarly when we change the selected Opportunity it should automatically update the value of the other fields accordingly

  • This sounds like it should be a Quick Action, not a New override. Zero code.
    – David Reed
    Feb 2, 2021 at 4:13
  • Can you please go over it, I want the new button to be on the record list page,but how exactly do i get the rest of the fiels autopopulated in the display,can you please go over
    – victor
    Feb 2, 2021 at 4:32

1 Answer 1


The actual problem here is that you're populating the data incorrectly:

<apex:inputText id="studentId" value="{!Withdrawal__c.Opportunity_Contact__r.Student_ID__c}"  />

You need to be able to access the data through the field path above. But in Apex, you're setting it up:

Opportunity Opp = [Select Id, Student_ID__c From Opportunity where Id = :sa.Opportunity_Contact__c];

sa.Opportunity_Contact__c = Opp.Id;

Assigning an Id doesn't get you any related fields; the data you just queried in Opp doesn't become part of the "chain" from sa. You'll probably just want to store the Opportunity as an instance variable and reference it by name, instead of using the field path through Withdrawal__c.

My suggestion to use a Quick Action doesn't exactly parallel your requirement, but you could use a Quick Action on the Opportunity to create a Withdrawal that would automatically populate the relationship to Opportunity, plus using Predefined Field Values to source any Opportunity values that are desired on the new record. You would have to write no code at all to achieve this; it just wouldn't be a button override.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .