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
Haystack CertifiedHaystack 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;
  1. Does the update to Type (tmp.Type = 'Customer') apply to all the records in the batch?
  2. Does the condition (tmp.Industry == 'Electronics')  get evaluated for individual records in the batch?
  3. 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.
Best Answer chosen by Haystack Certified
William TranWilliam Tran
Does the update to Type (tmp.Type = 'Customer') apply to all the records in the batch? -> No, you'll likely get an error, you need to iterate through each object

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

William TranWilliam Tran
Does the update to Type (tmp.Type = 'Customer') apply to all the records in the batch? -> No, you'll likely get an error, you need to iterate through each object

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.

 
This was selected as the best answer
Pramodh KumarPramodh Kumar
Hello sir,

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;