Inserting records i...
 
Notifications
Clear all

[Solved] Inserting records into multiple objects in Salesforce with a single DML statement

2 Posts
1 Users
0 Likes
550 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

How to insert records into two or more objects with one single dml statement in Salesforce?

Issue Tags
dml
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

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;
Reply
Share: