+ Start a Discussion
Roshan TamrakarRoshan Tamrakar 

Open VF Page in 'Add' mode

I want to be able to create new record with some fields pre-populated with default value and display to user ready for save. The new record window I want to invoke from a class. For my similar post earlier, Ron Hess has suggested me to use visualforce page with custom controller and extension to read parameter and assign them to new case. Below is what I tried.

 

Test_Object__c

Name

Log_Date__c

Score__c

 

TestObjectPage.page

Code:
<apex:page standardController=" Test_Object__c" extensions="TestObjectExtension">
  <apex:pageBlock>
    Test Object Name = {! Test_Object__c.Name}
    PrmName = {!prmName}
  </apex:pageBlock>
  <apex:detail/>
</apex:page>

 


This displays my object’s detail page in detail window.

 

TestObjectExtension.cls

Code:
public class TestObjectExtension{
            private final Test_Object__c t;
            String name;
            public TestObjectExtension(ApexPages.StandardController stdController){
                        this.t = (Test_Object__c)stdController.getRecord();
                        String prmName = System.CurrentPageReference().getParameters().get('name');
                        this.name = prmName;
                        t.Name = prmName;
                        System.debug('abcd');
                        System.debug(prmName);
            }
            public String getPrmName(){
                        return this.name;
            }
}


 

To use this page in ‘View’ mode, I overrode ‘View’ button with my VF page. When I click any record in this object, my VF page is displayed correctly. It also executes my class.

 

But as per my requirement, I overrode ‘New’ button with my VF page so that when a new record is being created, my VF page is displayed so that I could pre-populate some of the fields.

When I click on the ‘New’ button, it returns error as ‘Unable to Access Page’.

Do I need to override the button at all? How could I call my VF page to open in ‘add’ mode and populate some of the fields?

 

Please help

 

 

jwetzlerjwetzler
You're getting that error because you have a detail component on your page, and the detail component expects that you are giving it a Test_Object__c id.  In the case of creating a new object, you do not have an id to give it, and the component throws an error.  We should have a better error message here, but know that you can't use a detail component for an object that does not yet have an id.

As for populating the fields, the solution you have will work as long as a name parameter is passed in.  So I guess the best way to do this (provided that the value has to be passed in by a parameter and is not something static you can put in your controller), you will have to override the tab page and provide your own "new" action, that basically returns the edit page (for accounts it is 001/e) with a name parameter.