Gentlemen, simple question. Does Apex wait for the DML operation of inserting accounts to complete before moving onto the next line (SOQL statement)?

insert parentAcctList;

List<Id> acctIdList = [SELECT id
                       FROM Account];

for(Id acctId : acctIdList) {
     Contact cont = new Contact(lastName = 'Lee', 
                                firstName = 'Robert', 
                                title = 'General', 
                                AccountId = acctId);
     insert cont; 
  • Yes. Apex executes line by line. – Krishna Bidwai Jul 22 '16 at 11:31


The code as you've provided is not considered 'bulkified' because it will execute an insert for each acctId.

You'd probably want to add the Contacts to a list, then insert that list outside of your for loop.


List<Contact> contactsToInsert = new Map<Contact>();

for(Id acctId : acctIdList) {
    contactsToInsert.add(new Contact(lastName = 'Lee', firstName = 'Robert'));

insert contactsToInsert;
  • Thank you for the answer! Yes, I am aware its not bulkified, just some code to frame the question about the account DML insertion. – Steel Reserve 211 Jul 22 '16 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.