Updating because my question was not clear:

I'm trying to override the new and save functionality of a Custom Object on Visualforce page via a controller extension.

Here's my .vfp code:

<apex:page standardController="CustomObj__c" extensions="CustomObjExtension">
  <apex:form >  
    <apex:pageBlock title="Create a New Grouping">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{! saveNewGroup }"/>
            <apex:commandButton value="Cancel"/>

        <apex:pageBlockSection columns="1">
            <apex:selectList label="Groups" value="{! CustomObj.group_id__c}" size="1">
                <apex:selectOptions value="{! CommunityGroups }" />
            <apex:inputField label="Chute Album Shortcut" value="{! CustomObj.group_name__c }" />

and for my extension:

public class CustomObjExtension {
  private ApexPages.StandardController controller {get; set;}

  public CustomObjExtension(ApexPages.StandardController controller) {
      this.controller = controller;

  public PageReference saveNewGroup(){
      //accessing CustomObj here and do things before saving

      return null;

  public List<SelectOption> getCommunityGroups() {
      List<Group> groups = [SELECT Id, Name FROM Group];
      List<SelectOption> options = new List<SelectOption>();

      for (Group g : groups) {
            options.add(new SelectOption(g.Id, g.Name)); 

   return options;

Before I save the new Grouping, I want to access the CustomObj object to change some data before saving. How do I do that? I've looked at a number of documents and forums with different ways of doing it, but none of them has worked for me yet.

I tried to add a {get; set;} for the customObj but that didn't seem to work.

What am I missing?

1 Answer 1


Using the StandardController methods you can access the Object:

private CustomObj__c theObject;

public CustomObjExtension(ApexPages.StandardController controller) {
      this.controller = controller;
      theObject = (CustomObj__c)controller.getRecord();

Then you can access it and set values as needed


  • but this returns a record based on the id of the query string? I'm trying to create an entirely new custom object record. Basically, -> New form -> Fill form -> Save new stuff. Sorry if my question was not clear.
    – kev_panda
    Nov 9, 2016 at 19:42
  • Then create an instance of that Object and populate it. If you are overriding the new button there should be no ID in the URL thus you will get the SObject from the controller as expected
    – Eric
    Nov 9, 2016 at 19:44
  • doh, so obvious. Thanks! I was not thinking right, but this did give me an 'Aha' moment. Thank you so much.
    – kev_panda
    Nov 9, 2016 at 20:38

You must log in to answer this question.

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