# Apex bulk coding style to update a field from a list of queried records

public class SOupdate{
public void display(){
List<invoice_statement__c> inv =
[Select
Status__c
From
Invoice_statement__c
Where Status__c='open'];

for(invoice_statement__c inn : inv){
inn.status__C = 'pending';
}
update inn;
}
}

-
Welcome to salesforce.stackexchange Ani. I've formatted your code for you (using the {} button in edit mode). Could you update your question to also say what is going wrong and what you may have already tried to do ? Only code is not much for us to go on. Help us help you. –  Samuel De Rycke Apr 8 '14 at 14:07
Thank you so much -just adding the {} made it work. –  Ani Apr 8 '14 at 14:29
I hope you didn't put the update dml statement inside your for-loop Ani, that will update the database on every iteration and salesforce will throw a governor error. Update the inv colletion in one single statement (see dphil's answer). –  Samuel De Rycke Apr 8 '14 at 14:31
Thanks for your help-I saw that! –  Ani Apr 8 '14 at 14:36
You can select any answer as the one that helped you most by clicking the(V) button on the left of it. That'll reward the poster with some additional reputation and indicate to other users that your question has been solved :-) –  Samuel De Rycke Apr 8 '14 at 14:45

Glancing at your code, I can tell it won't compile. You are trying to update a variable that does NOT exist at that point.

 for(invoice_statement__c inn : inv)
inn.status__C = 'pending';
update inn;


for(invoice_statement__c inn : inv)
{
inn.status__C = 'pending';
}
update inv;


You should really be using update inv; instead. You are setting the value and then updating all of the records at once instead of 1 at a time.

for(invoice_statement__c inn : inv)
{
inn.status__C = 'pending';
update inn;//DO NOT DO THIS
}


This would compile, but don't do it this way. You will hit governor limits when there are multiple records because you have a limited number of DML statements you can execute within a single iteration of code. Doing DML inside a loop will cause that to happen.

-
Thank you Dphil!! –  Ani Apr 8 '14 at 14:44

In addition to dphil's corrections, you could also perform this logic by simply creating a workflow rule of whenever an Invoice Statement is created/updated to Open status, then peform a workflow field update to set the status to 'Pending'.

-
Definitely depends on the use case they have, but +1 anyways for an additional option since we don't have full info about their needs. –  dphil Apr 8 '14 at 14:25