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, 3 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

    How do you Integrate Salesforce with Alchemer?

    How do you Integrate Salesforce with Alchemer?

    Blog in Others

    Salesforce is a popular customer relationship management (CRM) platform, and Alchemer is a survey and data collection tool. Formerly known as SurveyGizmo, the platform offers…

    Customer Experience, Customer Relationship Management, Integrate Salesforce, Integrate Salesforce CRM, Online communities
    ashutosh pandey Feb 27, 2023
    1,403  Views
    Defining-Business-Processes-In-Salesforce

    Best Practices For Defining Business Processes In Salesforce

    Blog in Sales

    The meaning of the term business process and the advancement of this definition since its origination by Adam Smith in 1776 has prompted such territories…

    Business Process
    William Feb 26, 2019
    6,163  Views

    Why Growing Businesses Are Shifting to Custom CRM Development

    Blog in Salesforce

    Summary As businesses grow, their CRM needs evolve beyond what generic tools can handle. This blog explores why many are choosing Custom CRM Solutions—covering key…

    CRM, Salesforce, Salesforce CRM
    William Jul 7, 2025
    124  Views

    Popular Salesforce Videos

    2020 Salesforce Admin Certification - Lightning Knowledge Base Setup

    2020 Salesforce Admin Certification - Lightning Knowledge Base Setup

    Video in Salesforce Certifications

    A Salesforce Admin Certification is the highest form of recognition a Salesforce Administrator can achieve; it demonstrates expertise in the field as well as a…

    Salesforce Certification, Salesforce Lightning, Salesforce Administrator, Salesforce Admin, Salesforce Video
    Abhishek Oct 16, 2020
    2,081  Views
    Rise & Shine with The Salesforce Women's Entrepreneur Group

    Rise & Shine with The Salesforce Women's Entrepreneur Group

    Video in Salesforce Stories

    The Salesforce Women’s Entrepreneur Group invites you to our first quarterly event of the year, Rise & Shine - Celebrating Women’s History Month. We are…

    Salesforce Training, Salesforce Tutorial, Salesforce Ecosystem, Salesforce Video, Salesforce Learning
    Nauman Mar 30, 2021
    1,483  Views
    Build a Simple Screen Flow with Salesforce Flow Builder

    Build a Simple Screen Flow with Salesforce Flow Builder

    Video in Others, Salesforce Stories, Salesforce Training

    Did you know that you can build interactive custom components without any code? Flow Builder is a powerful tool for automation and for giving Admins…

    Salesforce Training, Salesforce Flow, Salesforce Admin, Salesforce Video, Admins
    Nauman Jun 16, 2020
    1,856  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

    © 2025 - 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.