Notifications
Clear all
Nov 17, 2021 10:09 am
How to insert records into two or more objects with one single dml statement in Salesforce?
1 Reply
Nov 17, 2021 10:10 am
Here is an example. Insert Account and Contact using one insert statement:
Account a = new Account(Customer_ID__c='123',name='account1'); Contact c = new Contact(account=new Account(Customer_ID__c='123'),lastname = 'test', externalId__c='extId'); insert new List<sObject>{a, c};
Inserting records into 3 objects - Account, Contact and a custom object (Accountcontact) which has lookup to Contact:
List<sObject> records = new List<sObject>(); records.add(new Account(AccountExternalId__c='1234', Name='ABC Company')); records.add(new Contact(account=new Account(AccountExternalId__c='1234'),lastname = 'Testlast', ContactExternalId__c='3525')); records.add(new AccountContact__c (Contact__r = new Contact(ContactExternalId__c='3525'))); insert records;