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
MattAustinMattAustin 

Override New button with visualforce and set field values

Can I use visualforce to set default values on a standard page layout?  I want to set the Opportunity Name field to a default value when the "new" button is clicked (and before the "save" button is clicked).   I am not sure what the visualforce command should look like to accomplish this task.  Is this possible?  Does anyone have a code snippet they could share?
 
Thanks
jwetzlerjwetzler
Right now you can't accomplish this and still keep your page layout control. You can however override the New Opportunity page  with your own visualforce page, and construct your layout using pageBlocks and inputFields.  Then you can use a controller extension to set the default field in your constructor. 

Code:
<apex:page standardController="opportunity" extensions="OppExt">
<apex:form >
  <apex:pageBlock title="Opportunity Edit" mode="edit">
    <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}"/>
        <!-- more buttons here -->
    </apex:pageBlockButtons>
    
    <apex:pageBlockSection title="Opportunity Information">
        <apex:inputField value="{!opportunity.name}"/>
        <!-- fill this section with all of the inputFields you want to display -->
    </apex:pageBlockSection>
    <!-- create more sections in this block -->
  </apex:pageBlock>
</apex:form>
</apex:page>

 
Code:
public class OppExt {

    public OppExt(ApexPages.StandardController controller) {
        Opportunity opp = (Opportunity) controller.getRecord();
        opp.name = 'My Default Name';
    }

}

 


MattAustinMattAustin
Thankyou!!  That worked great!  (my first visualforce page...  I'm so exited!! :smileyhappy:)