0

I am a bit stuck. I have tweaked many VF pages that have custom controllers but I haven't really ever had to make one from scratch. I am stuck on the following item. If I just want to insert a new record into a custom object, no lookup or passing values around, what is the most basic syntax for a controller to create a custom record?

**EDIT: This will be shown as a sidebar VF page. **

Here is my VF page: ( I did start out using the standard controller for object.)

<apex:page Controller="Admin_Tracking_Controller" showHeader="false" sidebar="false">
    <style>
        //removed for post
    </style>

    <apex:form style="background-color:#D8DDE7;height:350px" >
        <div class="h1">
            Admin Time Tracking
        </div>
        <div class="b1">       
            <apex:outputText >Activity:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.Activity__c}"/>
            <br/><apex:outputText >Account:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.Account__c}"/>
            <br/><apex:outputText >The Hub:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.The_Hub__c}"/>
            <br/><apex:outputText >Carrier:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.Carrier__c}"/>
            <br/><apex:outputText >Time Utilized:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.Time_Utilized__c}"/>
            <br/><apex:outputText >Notes:</apex:outputText>
            <br/><apex:inputField value="{!Admin_Tracking__c.Tracking_Notes__c}" style="resize:none;height:50px;"/>
        </div> 

        <div class="b2" align="center" draggable="false" > 
            <apex:commandButton value="Save Time Entry" action="{!save}" />
        </div> 

    </apex:form>

</apex:page>

My best guess at controller:

public class Admin_Tracking_Controller {

  public Admin_Tracking_Controller() {

  }

}

Here is what this looks like with the standard controller: enter image description here

3
  • Are you going to override the "New" Action for this sObject, or is this VF Page separate from that? When the user clicks "Save Time Entry" are they redirected to a detail page, or do they stay on the same page?
    – dBeltowski
    Commented Nov 15, 2016 at 18:59
  • They stay on the same page. This is a side bar component.
    – Matt M
    Commented Nov 15, 2016 at 19:00
  • I just update the original post with a screenshot.
    – Matt M
    Commented Nov 15, 2016 at 19:02

1 Answer 1

1

Variable names can't have __ in them, so the most basic change would be to rename all those values:

<apex:page Controller="Admin_Tracking_Controller" showHeader="false" sidebar="false">
    <style>
        //removed for post
    </style>

    <apex:form style="background-color:#D8DDE7;height:350px" >
        <div class="h1">
            Admin Time Tracking
        </div>
        <div class="b1">       
            <apex:outputText >Activity:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.Activity__c}"/>
            <br/><apex:outputText >Account:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.Account__c}"/>
            <br/><apex:outputText >The Hub:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.The_Hub__c}"/>
            <br/><apex:outputText >Carrier:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.Carrier__c}"/>
            <br/><apex:outputText >Time Utilized:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.Time_Utilized__c}"/>
            <br/><apex:outputText >Notes:</apex:outputText>
            <br/><apex:inputField value="{!adminTracking.Tracking_Notes__c}" style="resize:none;height:50px;"/>
        </div> 

        <div class="b2" align="center" draggable="false" > 
            <apex:commandButton value="Save Time Entry" action="{!save}" />
        </div> 

    </apex:form>

</apex:page>

Then, you need a variable to store the data, and a save method:

public class Admin_Tracking_Controller {

  public Admin_Tracking__c adminTracking { get; set; }
  public PageReference save() {
      return new ApexPages.StandardController(adminTracking).save();
  }
}

This save method takes care of saving the record and redirecting to the new or existing record on success, or displaying errors on failure.

3
  • OP replied to my comment saying that the User would stay on the same page, not be redirected. Can you update your save method to reflect that?
    – dBeltowski
    Commented Nov 15, 2016 at 19:08
  • @sfdcfox How do I get the page to just refresh after being saved instead of being redirected to another page? If you look in my screenshot we are just using this for time logging so the user doesn't ever change pages.
    – Matt M
    Commented Nov 15, 2016 at 19:12
  • @MattM You could just use something like public void save() { insert adminTracking; adminTracking = new Admin_Tracking__c(); } Go ahead and play with it.
    – sfdcfox
    Commented Nov 15, 2016 at 19:42

You must log in to answer this question.

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