0

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:pageBlockButtons>

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

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

      this.controller.save();
      return null;
  }

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

      for (Group g : groups) {
         if(String.isNotBlank(g.Name)){
            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?

2

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

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardController_methods.htm

| improve this answer | |
  • 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 '16 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 '16 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 '16 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.