Pre-filling the form for SFDC custom objects


Custom objects represent an easy way to extend the standard data model of SFDC. In this post, we show how to pre-fill the form fields when a custom object is being created.

In this example, we introduce a new Custom object, called Case Feedback, linked to a standard Case object via Master-detail relationship, making Case a parent object. Case Feedback defines these attributes:

  • feedback – evaluation of the case processing
  • subject – original case subject
  • case – Master-Detail relationship

To improve the user experience, it is convenient to pre-fill in some form fields for a user. To achieve it, we need to create:

  • Visualforce page
  • Controller extension

Visualforce page

Visualforce page form should contain inputs for the all custom objects fields. There should be a button to save a new object or cancel the process and return to the parent object. Here’s how it looks:

<apex:page standardController="Case_Feedback__c" extensions="CaseFeedbackController" tabStyle="Case_Feedback__c" title="Create Case Feedback">
      <h1 style="text-align: center">Create Case Feedback</h1>    
      <apex:form >
          <apex:pageBlock >
              <apex:pageMessages />
              <apex:pageBlockSection > 
              <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Case subject" for="subject" />       
                  <apex:outputLabel value="{!Case_Feedback__c.Subject__c}" id="subject"/>       
              <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Feedback" for="feedback" />       
                  <apex:inputText value="{!Case_Feedback__c.Feedback__c}" id="feedback"/>       
          <apex:pageBlock id="pb">
              <apex:commandButton action="{!cancel}" value="Cancel" id="cancelButton"/>
              <apex:commandButton action="{!saveFeedback}" value="Save" id="saveButton"/>             

Controller extension

The logic for assigning initial values is contained in the controller extension. The form is backed by the new object instance upon which the insert operation is finally executed. To be able to set some values for this object in the extension, the extension needs this object reference. We retrieve it by writing a custom constructor with the Standard controller as the input argument. The only remaining part is to initialise those fields that can be done in an automated way.

public class CaseFeedbackController {
    private Case caseObject;
    private Case_Feedback__c feedback;
    private ApexPages.StandardController sc;
    public CaseFeedbackController (ApexPages.StandardController sc) { = sc;
        feedback = (Case_Feedback__c) sc.getRecord();
        // we query the database for the parent object
        caseObject = [SELECT Subject FROM Case WHERE Id = :feedback.Case__c];
        // initialization of the CaseFeedback instance goes here
        feedback.Subject__c = caseObject.Subject;
    public Case_Feedback__c getFeedback() {
        return feedback;
    public PageReference saveFeedback() {
        // validation goes here;                
        return redirectToParent();
    public PageReference cancel() {
        return redirectToParent();  
    private PageReference redirectToParent() {
        Pagereference page = new Pagereference('/' + caseObject.Id);        
        return page;

Test it!

With the Visualforce page and the extension in place, we can put it in practice by linking the New button of the custom object to the visual page via Object Manager > Case Feedback > Buttons, Links, and Actions.

about author

Miroslav Rusin

Software developer who enjoys travelling, playing guitar and some solid workout!


hotovo tech corner cover october

The Top Latest Web Dev Tips and Updates

Hello readers! Just as we do every month, we’ve pulled …

sanae software testing beer.ex conference

Sanae Software Testing BEER.EX 2022 Impressions

We love being testers. But what do we love the …

Hotovo tech corner september

Top Tips You Don’t Want to Miss

Hello readers! Just as we do every month, we’ve pulled …