function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Insert Child Record before insert of parent record



I am working on a requirement in which I am trying to insert a child record while creating the parent record.


My parent object is the Case Object and my child object is a Custom Object called People__c. Through the use of a Custom button, I would like to create a People__c record. Basically, when the user clicks the button on the case record, the user will be taken to the People__c object, all the necessary details can be filled in and once the people record is saved, the user will be redirected back to the case record that has not still been saved to the database.


I have never worked on a requirement like this before. Any suggestions or sample code would greatly help me out.




Hi ,


You can achieve this using intermediate Visualforce and Apex controller.


Use Content source is Visualforce Page, While creating a custom button for the Case object. And take a look of the model codings as shown below,


Visualforce Page

 Standard Controller is your Master object.


<apex:page standardcontroller="Contract_Summary__c" extensions="ConvertContractControl" action="{!convertcontract}">




public with sharing class ConvertContractControl {

public Contract_Summary__c cs{get;set;}   // Use your Master Object here.
public ContractAdmin__c ca{get;set;}  // This is child object.
    public ConvertContractControl(ApexPages.StandardController controller) {
public pageReference convertcontract()
cs=[select Community__c from Contract_Summary__c  where limit 1];
ca=new ContractAdmin__c(Community__c=cs.Community__c);
insert ca;
PageReference nextpage = new PageReference('/'; // If you need , you can redirect to the child object. or use can                                                                                              //use your master object id itself
        return nextpage.setRedirect(true);






Thanks Vennila for the code and your suggestion!

I tried out your code. Can you tell me what the Community__c field is in your code?

Is it a custom text field in your Master Object?

I am getting the
System.QueryException: List has no rows for assignment to SObject error.

Can you please point out my mistake?



Community__c is a Master-Detail Lookup  field in Child Object.


I think , you did this functionality at before inserting a record in Master object. 


But this code is running for After inserting a Parent record.  (Example : Lead Convert Button.)

My requirement is completely different, I need to insert my child record before inserting the parent record!

Do you happen to have any suggestions?

One way to accomplish this is to build a VF page for both (Case and People__c) and use the same controller class. That way, you hold onto the list of People__c objects you are creating, as well as the Case object you're creating.

In the end, you will need to save the Case first to get the ID and place onto the People__c records to link them to the Case.


public Case theCase { get; set; }

public List<People__c> People { get; set; }


When the user clicks the button to add a People__c, send them to the People__c VF page, but use "SetRedirect(false);" on the page reference. This will maintain your Case object and your List of People__c. You should also send a page parameter over so the controller know that it is adding a People__c.


When you return to the Case, use the "SetRedirect(false);" again.


When the user click on Save, Perform the Upsert on the Case, then get the ID and loop through the People and set the

People__c.Case__c = theCase.Id;