You need to sign in to do that
Don't have an account?
Novo_Artis
Dynamic DML (Referencing objects before insertion)
I have been wondering if it is possible to relate objects without inserting the parent first.
I hope to achieve something similar to this...
List<Contact> cList = new List<Contact>();
cList.add(new Contact(FirstName = 'Jeff'));
cList.add(new Contact(FirstName = 'John'));
cList.add(new Contact(FirstName = 'Joe'));
Account a = new Account(contacts = cList);
insert a;
I know I could insert the account, reference it in each contact then insert the contacts.
The issue with that is I have to use more DML statements and it also forces me to write lots of loops to handle bulk inserting of new objects with children.
Account a = new Account();
You cant do that becuase you need the Account Id before inserting the contacts.
Salesforce doesn't do it automatically.
Ignacio.
Novo,
You can bulk insert the Contacts though.
Account a;
...
insert a;
List<Contact> clist = new List<Contact>();
for(...)
clist.add(new Contact(name = 'xyz', AccountId = a.id));
insert clist.