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
JNicJNic 

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?

 

Best Answer chosen by Admin (Salesforce Developers) 
SPDSPD

it works because you are using  ' <=' ...in 1st case you were saying

 

for ( j = 0;j <= numNewCtcs; j++ ) {
Contact ctcToGet = newCtcs.get(j);
ctcToGet.AccountId = act.id;
}

 

 

 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 

for ( j = 0;j <= numNewCtcs-1; j++ ) 

 

the condition is satisfied and your code works fine.

 

 

Instead you can also use the following:

 

 

for ( j = 0;j < numNewCtcs; j++ )

All Answers

JNicJNic

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;

 

 any ideas please?

 

JNicJNic

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;

 

 

 

SPDSPD

it works because you are using  ' <=' ...in 1st case you were saying

 

for ( j = 0;j <= numNewCtcs; j++ ) {
Contact ctcToGet = newCtcs.get(j);
ctcToGet.AccountId = act.id;
}

 

 

 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 

for ( j = 0;j <= numNewCtcs-1; j++ ) 

 

the condition is satisfied and your code works fine.

 

 

Instead you can also use the following:

 

 

for ( j = 0;j < numNewCtcs; j++ )
This was selected as the best answer
JNicJNic

Thank you!

 

Your suggestion seems cleaner.