Toggle Side Panel

  • Blogs
  • 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
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • 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
    • 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 8 years, 2 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());
      }
      }

    • [adinserter block='9']
    • 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

    Popular Salesforce Blogs

    What is Salesforce CPQ and How Does it Work?

    Blog in Others

    Salesforce CPQ is a software solution that helps businesses automate and streamline their sales processes by providing a way to quickly and accurately create quotes,…

    B2B, B2B Organizations, B2B Quoting Process, B2C Use Case B2B, Bundles and Configuration
    ADVANZ Jan 18, 2023
    4,057  Views

    Account Based Marketing With Salesforce – 4 Proven Strategies

    Blog in Salesforce

    Account-Based Marketing is ever-growing and a widely practiced B2B Sales and marketing technique that helps acquire potential companies as your clients with networking. The first…

    B2B, B2B Companies, B2B Marketers, B2B Sales and Marketing, Businesses
    LevelShift (formerly DemandBlue) Sep 23, 2020
    2,467  Views
    Salesforce Essentials features, pricing, and limits overview

    Is Salesforce Essentials Right for Your Business? Features, Pricing & Limits

    Blog in Salesforce, Salesforce Consultant, Salesforce Tools

    Choosing a CRM is not simply about picking the platform with the longest feature list. For a small business, the right CRM needs to be…

    Salesforce, Salesforce Consulting
    Harshita Sep 25, 2026
    196  Views

    Popular Salesforce Videos

    Salesforce Salaries In Current Market?

    Salesforce Salaries In Current Market?

    Video in Salesforce Development

    Wondering about the Salesforce salaries in the current market. Join this useful group for Salesforce job & career info and learn all about the Salesforce…

    Salesforce Training, Salesforce Tutorial, Salesforce Jobs, Salesforce Developer, salesforce
    Intelogik Nov 7, 2022
    1,762  Views
    Revenue Sharing with Salesforce | Unlocking Profit Potential | Live Stream Highlights

    Revenue Sharing with Salesforce | Unlocking Profit Potential | Live Stream Highlights

    Video in Others

    This extract from our live webinar uncovered how this powerful platform can revolutionize your revenue generation and maximize your profits. Discover the untapped potential of…

    Salesforce Training, Salesforce Developer, salesforce, Salesforce Hulk, Salesforce Webinar
    Cyntexa Aug 31, 2023
    1,333  Views
    Salesforce DX simplified with AutoRABIT - Bay Area Salesforce Developer Group

    Salesforce DX simplified with AutoRABIT - Bay Area Salesforce Developer Group

    Video in Salesforce Implementation, Salesforce DX

    A common need that teams developing on the Salesforce platform express is the desire for a single solution that will take their developers through the…

    Salesforce Release Management, Salesforce Continuous Integration, Salesforce Git Integration, Salesforce Svn Integration, Salesforce Test Automation
    Pooja Jan 17, 2018
    2,296  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®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.