Consider a SOQL for loop

for(Account Acc : [Select id,Name FROM Account]){
   Acc.Name = 'Test';

Now how do I update the list of accounts I queried into? Is it possible? Thanks


Why don't you do?

Account[] aList = [select id, name from Account];
for (Account a: aList)
   a.name = 'Test';
update aList;

or, even better

Account[] aList = new List<Account>();
for (Account a : [select id, name from Account])
  aList.add(new Account (id = a.id, name = 'Test'));
update aList;
  • The first option is my current implementation. I wanted to take a swing a reducing variables - so thought about eliminating the list we query into. The second one is what I was looking for. Thank you. – Prajith May 3 '16 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.