I overrode the new button with a custom VisualForce page but the problem is when I click the new button on the related list (Cases) it use to pre-populate the contact and account names. Now that I have a custom page it does not. I tried URL hacking and I can't get it to pre-populate. Even if I did successfully get the URL hack to populate the field I don't think I would be able to tell the new button on the related to pass the parameters needed. The id is j_id0:j_id29:j_id30:referralBasics:accountId and whenever I add the query string to the url and put a value there it shows up blank.

So I then tried to just query everything I need and then populate the fields via my controller but now when they press save it doesn't save the fields that were prepopulated (because they are stored in controller variable instead on the Case Page variable). Here's what I have for the second option. It populates the fields but when saving it doesn't save correctly and I tried overriding the save but that isn't working correctly:

public class RHX_Custom_Master_New_Controller {
private ApexPages.StandardController sc;
public Account company {get; set;}
public Contact employee {get; set;}
public boolean hasEmployee{get;set;}
public boolean hasCompany{get;set;}

public RHX_Custom_Master_New_Controller.RHX_Custom_Master_New_Controller(ApexPages.StandardController controller) {
    sc = controller;
    List<Contact> cons = [select Name from Contact WHERE Id = :ApexPages.currentPage().getParameters().get('cas3_lkid') limit 1];
    if(cons.size() > 0) {
        employee = cons[0];
        hasEmployee = true;
    } else 
        hasEmployee = false;

    List<Account> accts = [select Name from Account WHERE Id = :ApexPages.currentPage().getParameters().get('cas4_lkid') limit 1];
    if(accts.size() > 0) {
        company = accts[0];
        hasCompany = true;
    } else
        hasCompany = false;

   // ApexPages.currentPage().getParameters().get('cas14')

public PageReference save() {
    PageReference detailPage = sc.save();
    // this is a syntax error but I need to change this case or get reference to the case being saved in the vf page and change the value here
    //Case.Contact.Name = employee.Name;

    if (detailPage != null) {
        // Construct URL of edit page or whatever other page you want
        PageReference editPage = new PageReference(detailPage.getUrl());
        return editPage;
    } else {
        return detailPage;


Here's the VF page section that is needed:

<apex:form >
<apex:pageBlock mode="edit" title="Master Referral Record Edit">
    <apex:pageBlockButtons location="both">
        <apex:commandButton value="Save" action="{!Save}"  />
        <apex:commandButton value="Cancel" action="{!Cancel}"  />
    <apex:pageBlockSection id="referralBasics" title="Referral Basics" columns="2" collapsible="true">
            <apex:inputField id="preferredFirstName" value="{!Case.Preferred_First_Name__c}"/>
            <apex:inputField id="owner" value="{!Case.Owner.Name}"/>
            <apex:inputField id="contactId" value="{!employee.Name}"/>
            <apex:outputText ></apex:outputText>
            <apex:inputField id="accountId" value="{!company.Name}"/>
            <apex:outputText ></apex:outputText>
            <apex:inputField id="parentId" value="{!Case.ParentId}"/>

Any ideas?

  • Look through the parameters that get sent from the related list button. You will need to parse those values from the page parameters and populate them through your extension.
    – Adrian Larson
    Commented Apr 9, 2015 at 21:11

1 Answer 1


The related list will already pass parameters from the standard button, so there is no need URL hack, you just need to know the parameter (which you would be using for URL hacking anyways) Then you assign the value to the record generated by the controller. Here is a an extremely simplified example.


public class OppEntryTestExtension {

  public OppEntryTestExtension(ApexPages.StandardController controller) {

    // Get Parameters that correspond to those passed by related list
    Id accountId = ApexPages.currentPage().getParameters().get('accid');

    // Reference to Controller Record
    Opportunity opp = (Opportunity)controller.getRecord();

    // Assign values to Record
    opp.AccountId = accountId;



Visualforce Page

<apex:page standardController="Opportunity" extensions="OppEntryTestExtension">

     <apex:inputField value="{!Opportunity.AccountId}" />


In this example we override the standard Opportunity New page and use the parameter passed by the Opportunity related list on the account to set the Account on the Opportunity.

Once you have the id of the incoming object, you could also query that object if there were other fields you wanted to pre-populate in your new related object. (for example, I wanted to use an Account Type field to set a type field on the Opportunity)

  • Hi what if i am creating opportunity from opportunity tab and select Account name it will auto populate all the fields so i want both the functionality if our user create opportunity directly from opportunity tab then fields should populate and if user creates opportunity from related list then it also populate . How to achieve these two functionality.
    – Cloud Arch
    Commented Jul 30, 2015 at 12:25
  • If the user creates an Opportunity directly from the opportunity tab, there is no account in context. How would the system know what Account should auto populate automatically?
    – CyberJus
    Commented Jul 30, 2015 at 12:48
  • in the opportunity tab there is an account lookup once user select account from that lookup some fields should auto populate. so basically i want both the functionality user can create opportunity from account page as well as from opportunity page (in this page user has to lookup that account manually )
    – Cloud Arch
    Commented Jul 30, 2015 at 12:54
  • You want other fields to populate when creating a new opportunity based on the selecting the account? That is a different scenario, this is auto populating when you enter the page. To populate after entering an account, you need to add an on change event, get your fields, then do a partial page refresh.
    – CyberJus
    Commented Jul 30, 2015 at 12:58
  • Yes you got it my first difficulty how to put these two functionality in one class my second difficulty is change event option only populate when all the fields are mandatory .How to get rid of it
    – Cloud Arch
    Commented Jul 30, 2015 at 14:08

You must log in to answer this question.

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