Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › Can we insert parent and child object record in single DML statement?

    Tagged: Account, Contact, Database, DML Statement, External ID, Parent Child Object

    • Salesforce® Discussions

      Can we insert parent and child object record in single DML statement?

      Posted by Himanshu on April 14, 2016 at 7:33 AM

      Can we insert parent and child object record in single DML statement?

      Avnish Yadav replied 7 years, 6 months ago 4 Members · 3 Replies
      • Account
      • Contact
      • Database
      • DML Statement
      • External ID
      • Parent Child Object
    • 3 Replies
    • Naman

      Member
      April 14, 2016 at 7:38 AM

      Try something like below: Note that reference field must be external id field on parent object. Here i have used SLASerialNumber__c  as external id field.

      Contact con = new Contact(lastName='bhardwaj');
      con.Email = 'naman.bhardwaj@test.com';

      Account accRef = new Account(SLASerialNumber__c='12');
      con.Account = accRef;

      Account acc = new Account(Name='namanTestAcc', SLASerialNumber__c='12');

      Database.SaveResult[] results = Database.insert(new SObject[] {acc, con});

      for (Integer i = 0; i < results.size(); i++) {
      if (results[i].isSuccess()) {
      System.debug('Successfully Created ID: '+ results[i].getId());
      }
      }

    • Parul

      Member
      September 7, 2018 at 4:24 AM

      To insert the parent-child object record in single DML statement as follows:

      Inserting Custom objects (Parent and Child) records in a Single DML Statement:
      here assume Parent__c and Child__c is a custom object:
      create a External Id in Parent__c object called Parent_Ref__c.

      Child__c c = new Child__c();
      c.Name = 'ChildRecord1';
      Parent__c parentReference = new Parent__c(p.Parent_Ref__c = 'abc1234');
      c.Parent__r = parentReference;
      Parent__c p = new Parent__c();
      Name ='TheBlogReaders';
      p.Parent_Ref__c = 'abc1234';
      Database.SaveResult[] results = Database.insert(new SObject[] { p, c });

       

      Thanks.

    • Avnish Yadav

      Member
      September 7, 2018 at 10:05 AM

      Hello,

      Yes, you need to make one field as an External id on your parent object.

      Thanks.

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    External Objects

    External Objects in Salesforce - The Implementation Guide

    Blog in Others, Salesforce, Salesforce Implementation

    External Object is a kind of virtual object which does not exist in your Org. It is a custom object like other custom objects we…

    API Version, App Button, App Name, Consumed Storage, Custom Tabs
    Shweta Nov 5, 2020
    9,201  Views

    How to use Amazon AppFlow to transfer data between Salesforce and AWS

    Blog in Data Migration

    Managing the flow of data between AWS and Software-as-a-Service SaaS applications like Salesforce can be difficult, time-consuming, and expensive. Developers can often spend months writing…

    API Access, App Policies, AppFlow, AppFlow Data, AWS
    Empirical Data Sep 29, 2020
    14,029  Views
    email studio

    Email Studio in Salesforce Marketing Cloud | The Ultimate Guide

    Blog in Salesforce Cloud Platform

    Email Studio is a very important part of the Salesforce Marketing Cloud, it is used to build and send personalized emails from basic newsletters to…

    Application, Buttons, Campaigns, Clicks, Dashboard
    Manish Jul 23, 2020
    23,945  Views

    Popular Salesforce Videos

    Salesforce Beginner Interview Questions and Answers Full Length Video [2023]

    Salesforce Beginner Interview Questions and Answers Full Length Video [2023]

    Video in Salesforce Training, Salesforce Admin

    Watch this video to get to know about Salesforce Beginner Interview Questions and Answers Full-Length Video [2023]. 00:00:00 Introduction 00:02:56 Sandbox 00:05:05 Process Builder 00:09:00…

    Salesforce Training, Salesforce Tutorial, Salesforce Certification, Salesforce Developer, salesforce
    CRS Feb 24, 2023
    1,653  Views
    Successfully Retrieving Metadata From Salesforce org Using Packages

    Successfully Retrieving Metadata From Salesforce org Using Packages

    Video in Salesforce Stories, Salesforce Training, Trailhead

    In this session, we will uncover various strategies that developers can use to pull metadata from Salesforce org successfully. Watch this video and learn everything.…

    Salesforce Training, Salesforce Tutorial, Salesforce Org, Salesforce Developers, Salesforce Video
    Neha Yadav Dec 9, 2020
    1,363  Views
    How to Take Your Salesforce Support Model to the Next Level

    How to Take Your Salesforce Support Model to the Next Level

    Video in Salesforce Stories

    Jeremy Muldoon, the Salesforce expert at Rimini Street, discusses the Salesforce resource challenges faced by businesses today, how to overcome them, and how to take…

    Salesforce Training, Salesforce Tutorial, salesforce, salesforce platform, Salesforce Video
    Nauman Feb 11, 2022
    1,806  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.