You need to sign in to do that
Don't have an account?
Haystack Certified
SOQL FOR Loops Questions
I have some questions on the special SOQL FOR loop using the List version.
//Example for (Account[] tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) { tmp.Type = 'Customer'; if( tmp.Industry == 'Electronics' ) { tmp.Rating = 'High'; } } update tmp;
- Does the update to Type (tmp.Type = 'Customer') apply to all the records in the batch?
- Does the condition (tmp.Industry == 'Electronics') get evaluated for individual records in the batch?
- Unrelated to the example above, is this a way to handle governor limits when updating numerous child records? For example, let's say a trigger on Account needs to update related Contact records and each Account has 50 Contacts. If a single batch update of 200 Accounts occurs, then 10,000 Contact records could be updated without hitting any limits? It sounds like this would use only one SOQL query and one Update statement to save to the database.
Does the condition (tmp.Industry == 'Electronics') get evaluated for individual records in the batch? No, you'll likely get an error, you need to iterate through each object
Unrelated to the example above, is this a way to handle governor limits when updating numerous child records? For example, let's say a trigger on Account needs to update related Contact records and each Account has 50 Contacts. If a single batch update of 200 Accounts occurs, then 10,000 Contact records could be updated without hitting any limits? It sounds like this would use only one SOQL query and one Update statement to save to the database.
Yes, you could bulkify. You'll likely have many SOQL queries (one per account) but can have on update statement for all contacts of all accounts.
Thx.
All Answers
Does the condition (tmp.Industry == 'Electronics') get evaluated for individual records in the batch? No, you'll likely get an error, you need to iterate through each object
Unrelated to the example above, is this a way to handle governor limits when updating numerous child records? For example, let's say a trigger on Account needs to update related Contact records and each Account has 50 Contacts. If a single batch update of 200 Accounts occurs, then 10,000 Contact records could be updated without hitting any limits? It sounds like this would use only one SOQL query and one Update statement to save to the database.
Yes, you could bulkify. You'll likely have many SOQL queries (one per account) but can have on update statement for all contacts of all accounts.
Thx.
where ever you looping through the list of records please try to use bulkify your code and here is the lin for the best practise in apex develpment.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
list<account> acc = new list<account>();
for (Account tmp : [SELECT Id,type,industry,rating FROM Account WHERE Name = 'yyy']) {
tmp.Type = 'Customer';
if( tmp.Industry == 'Electronics' ) {
tmp.Rating = 'High';
}
acc.add(tmp);
}
update acc;