function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
textualtextual 

how best to add parent child relationships

right now im doing this

for(Integer i = 0 ; i < numOfAccounts; i++){
   Account tempAccount = new Account(Name = accountPrefix + '_test_' + i);
   tempAccount.BillingStreet = string.valueof(i) + ' test street';
   tempAccount.BillingCity = 'cityville';
   tempAccount.BillingState = 'aa';
   tempAccount.BillingPostalCode = '12345';
   tempAccount.BillingCountry = 'USA';
   accounts.add(tempAccount);
  }
  insert accounts;

for(integer x = 0 ; x < Accounts.size(); x++){
   // create account contats
   // emails will be username+icann_test_X_Y@gmail.com where X is number of account and Y is number of contact
   for(Integer y = 0 ; y < numOfContactsPerAccount; y++){
    Contact tempContact = new Contact(firstName = 'First_' + y, LastName = 'Last_' + y);
    tempContact.accountId = accounts[x].id;
    tempContact.email = gmailAccount + '+_test_' + x + '_' + y + '@gmail.com';
    contacts.add(tempContact);
   }
  }
  insert contacts;

Is there a way to add the map the accounts before theyre inserted so that when i create the contacts the id gets auto populated?
or should i continue with this method to add the parents, get the ids, loop through them and add children as needed?
GunnarGunnar
You have to write the records to get an id.
You need the id to create the relationship.
So it looks like a 2-step operation to me.
GunnarGunnar
3-step.

(1) Write.
(2) Read to get id's.
(3) Create the contacts using id's from step 2.