Tech Corner - 6. September 2017
header_image

Pre-filling the form for SFDC custom objects

Introduction

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:pageBlockSectionItem >
                  <apex:outputLabel value="Feedback" for="feedback" />       
                  <apex:inputText value="{!Case_Feedback__c.Feedback__c}" id="feedback"/>       
              </apex:pageBlockSectionItem>
  
              </apex:pageBlockSection>
          </apex:pageBlock>
           
          <apex:pageBlock id="pb">
              <apex:commandButton action="{!cancel}" value="Cancel" id="cancelButton"/>
              <apex:commandButton action="{!saveFeedback}" value="Save" id="saveButton"/>             
          </apex:pageBlock>
      </apex:form>   
</apex:page>

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) {
        this.sc = sc;
        feedback = (Case_Feedback__c) sc.getRecord();
        // we query the database for the parent object
        caseObject =;
        // 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
        sc.save();                
        return redirectToParent();
    }
      
    public PageReference cancel() {
        return redirectToParent();  
    }    
      
    private PageReference redirectToParent() {
        Pagereference page = new Pagereference('/' + caseObject.Id);        
        page.setRedirect(true);        
        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 the author

Miroslav Rusin

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

READ MORE