Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Creating a Child Record When a Parent Record is Created

Creating a Child Record When a Parent Record is Created

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice


You want to automatically create a new child record when you create a parent record. The child record should be populated with default values from the position.


Use an Apex trigger to automatically create the child record when a new parent record is created.

For this example, let's automatically create a new interviewer record (child) for the specified hiring manager whenever a new position (parent) is created.

trigger AutoCreateInterviewer on Position__c (after insert) {
    List<Interviewer__c> interviewers = new List<Interviewer__c>();

    //For each position processed by the trigger, add a new  
    //interviewer record for the specified hiring manager.  
    //Note that Trigger.New is a list of all the new positions  
    //that are being created.  
    for (Position__c newPosition: Trigger.New) {
        if (newPosition.Hiring_Manager__c != null) {
            interviewers.add(new Interviewer__c(
                        Name = '1',
                        Position__c = newPosition.Id,
                        Employee__c = newPosition.Hiring_Manager__c,
                        Role__c = 'Managerial'));
    insert interviewers;


Recipe Activity - Please Log in to write a comment

I verified

voted as verified by PS001  (2014-07-16)

nice example to understand the integration part

voted as verified by Praneel PIDIKITI  (2013-01-21)

I would also like to see what the test class would look like.  I'm struggling with writing unit tests.. :(

by phoule03122012  (2013-01-07)

Can anyone answer Sayame? I need to write a test class for this trigger as well. 

by a093000000WbnhY  (2013-01-04)

How do you write a test class for this trigger?

by Sayame  (2011-04-28)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.