+ Start a Discussion
could & rockcould & rock 

​Any risk from following for loop code

Code Piece A

Account[] accs = [Select .... from Account where ... Limit 30000];
Account[] accsToUpdate = new Account[]{};
for (Account acc: accs) {
if (...) {
accsToUpdate.add(acc);
}
}

update accsToUpdate;


Code Piece B
Account[] accsToUpdate = new Account[]{};
for (Account acc: [Select .... from Account where ... Limit 30000]) {
if (...) {
accsToUpdate.add(acc);
}
}

update accsToUpdate;

For code priece A, I know there is a risk of running into heap size limit because of saving big data into array; what about code priece B? Will SFDC user QueryMore to implement the for loop in which data fetched later on could override data fetched previously and saved in heap?
SaranSaran
Hi,

Processing the for loop will not get you into any issue. 

 If you are saving the result in the list it will retrive only 50,000 records.

Since you are processing only 30,000 records it will not be a issue.

But the problem is during the updation. We can perform DML operation for only 10,000 records. In both the if your if condition is satisfied for all the 30,000 records you will get error.

limit link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Instead you can write a batch class while helps to solve your problem.

Hope this helps you.
Thanks