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

  • 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 = '[email protected]';

    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());
    }
    }