You need to sign in to do that
Don't have an account?

Help updating items in a list before the list is inserted
Hey guys,
I've got a controller that has two lists:
newLocs (Location__c custom obj)
newCtcs (Contact obj)
I've got a VF page that allows users to create an Account, nContacts, and nLocs. But I need to relate the contacts and Locations to my account when I hit my custom save button.
I don't know how to modify the contents of thoses lists and THEN insert them.
ie: I want to modify all records in newLocs list to have the Account__c field of my newly inserted account.
Here is my controller so far:
public with sharing class accountController { public Account act {get;set;} public List<Contact> newCtcs {get;set;} public List<Location__c> newLocs {get; set;} // ... some other code // public PageReference saveStep1() { try { //Lots to do here! - First insert the account insert act; //Then insert the contacts
//before I do this, I want to change all the AccountId fields to be act.id
insert newCtcs; //Then insert the locations
//before I do this, I want to change all the Account__c fields to bt act.id
insert newLocs; } catch (Exception e) { ApexPages.addMessages(e); } return null; } }
Can someone help me out?
it works because you are using ' <=' ...in 1st case you were saying
so if your list contains 10 records then...its size will be 10,but since the indexing in a list starts from 0,you have to itereate till '9'(i.e iterate 10 times).. but here you iterate 11 times(till 10th index) whereas the size of your list is 10..so it said list out of bounds..
when you write
the condition is satisfied and your code works fine.
Instead you can also use the following:
All Answers
Ok... got a little further... but now this gives me "list out of bounds 1"
public PageReference saveStep1() { try { //Lots to do here! - First insert the account insert act; //Then insert the contacts //need to figure out how to put the act.id into each record in newCtcs integer i, j; integer numNewCtcs = newCtcs.size(); for ( j = 0;j <= numNewCtcs; j++ ) { Contact ctcToGet = newCtcs.get(j); ctcToGet.AccountId = act.id; } insert newCtcs; //Then insert the locations //insert newLocs; } catch (Exception e) { ApexPages.addMessages(e); } return null;
Got it!
Don't know why this works... but here it is:
public PageReference saveStep1() { try { //Lots to do here! - First insert the account insert act; //Then insert the contacts //need to figure out how to put the act.id into each record in newCtcs integer i, j; integer numNewCtcs = newCtcs.size(); for ( j = 0;j <= numNewCtcs-1; j++ ) { Contact ctcToGet = newCtcs.get(j); ctcToGet.AccountId = act.id; } insert newCtcs; //Then insert the locations //insert newLocs; } catch (Exception e) { ApexPages.addMessages(e); } return null;
it works because you are using ' <=' ...in 1st case you were saying
so if your list contains 10 records then...its size will be 10,but since the indexing in a list starts from 0,you have to itereate till '9'(i.e iterate 10 times).. but here you iterate 11 times(till 10th index) whereas the size of your list is 10..so it said list out of bounds..
when you write
the condition is satisfied and your code works fine.
Instead you can also use the following:
Thank you!
Your suggestion seems cleaner.