I have 3 objects. Account, Contact and AccountContact(custom).
When I try to insert account and contact using one insert statement as follows, it works perfectly
Account a = new Account(Customer_ID__c='12345',name='accounttest');
Contact c = new Contact(account=new Account(Customer_ID__c='12345'),lastname = 'test', externalId__c='extId');
insert new List<sObject>{a, c};
Now, I am trying to insert custom object(Accountcontact) which has lookup to contact as follows:
Account a = new Account(Customer_ID__c='12345',name='accounttest');
Contact c = new Contact(account=new Account(Customer_ID__c='12345'),lastname = 'test', externalId__c='extId');
AccountContact__c ac = new AccountContact__c(name='testing', Contact__c = new Contact(externalId__c='extId'));
insert new List<sObject>{a, c, ac};
Above code throws an error "Invalid initial expression type for field AccountContact__c.Contact__c, expecting: Id
"
Is there a way to achieve inserting all 3 in single DML call. Thanks