I have a scenario wherein I have two custom object records of the same type, ObjectX__c. I need to update object A (which exists) with a lookup to object B (which doesn't yet exist and is being inserted in this transaction). I want to avoid having to insert object b, then get the Id and then update object A.

In the past, I have used the approach outlined here to accomplish this, however since these two records are of the same sobject type, this will not work.

Does anyone have any suggestions or workarounds to this approach, the goal being to accomplish this in ideally one transaction with one dml?

1 Answer 1


Try this as an example,

Account a = new Account(Name='Test Account');
Contact c = new Contact(Firstname='Test', Lastname='Johnny');    
Database.insert( new List<sObject> {(sObject)a, (sObject)c} );
  • I need to update an existing record + insert a record and they are the same sobject type. Your response indicates two inserts and of two different sobject types. Thank you though.
    – GeorgeA
    Commented Mar 25, 2019 at 17:02
  • Ah I see, In that case your out of luck, there is an idea out there though success.salesforce.com/ideaView?id=08730000000aNhVAAU. Has 7 years on it.. oddly enough it's under the point threshold. Commented Mar 25, 2019 at 17:31
  • 7 years is never promising haha
    – GeorgeA
    Commented Mar 25, 2019 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .