ChildObj__c has a Master-detail relationship with ParentObj__c. The need is to save ParentObj__c and ChildObj__c records in a visualforce which has ParentObj__c as standard controller. I'm trying to accomplish this using extension class. So far, both records are saved and the parent record creation seems to work as expected, but the problem is with the child object's fields ChildField1__c, ChildField2__c, and ChildField3__c. These field doesn't have any values in the new child record. Where are the entered Child object values escaping? Am I missing something? Can this be fixed or is there a different approach to handle this? Thanks in advance.


<apex:page standardController="ParentObj__c" extensions="ExtensionClass" >
    <apex:form >
        <apex:pageBlock title="Parent Block">
            <apex:pageBlockSection >
                <apex:inputField value="{!ParentObj__c.ParentField1__c}" required="true" />
                <apex:inputField value="{!ParentObj__c.ParentField2__c}" />
        <apex:pageBlock title="Child Block">
            <apex:pageBlockSection >
                <apex:inputField value="{!ChildA.ChildField1__c}" required="true" />
            <apex:pageBlockSection >
                <apex:inputField value="{!ChildA.ChildField2__c}" required="true" /><!-- Controlling picklist -->
                <apex:inputField value="{!ChildA.ChildField3__c}" required="true" /><!-- Dependent picklist -->
        <apex:commandButton value="Submit" action="{!SaveRecord}" />


public class ExtensionClass {

    public ParentObj__c ParentA {get;set;}  
    public ChildObj__c ChildA {get;set;}    

    public ExtensionClass(ApexPages.StandardController std) {
        ParentA = (ParentObj__c) std.getRecord();

    public PageReference SaveRecord() {

        insert ParentA;

        ChildA = new ChildObj__c();
        ChildA.ParentObj__c = ParentA.Id;
        insert ChildA;

        PageReference pr = new PageReference('/apex/SomePage');
        return pr;

Seems to me that you need to instantiate ChildA in the constructor of ExtensionClass. The way it is now, you create a new instance of ChildObj__c named ChildA upon save, and the data previously collected from the user is lost.

  • Best! You filled the gap and made it work. – Seesh Sep 15 '17 at 5:37

Your Answer

