Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
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;
    }
}
share|improve this question
1  
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 at 14:07
    
Thank you so much -just adding the {} made it work. –  Ani Apr 8 at 14:29
1  
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 at 14:31
1  
Thanks for your help-I saw that! –  Ani Apr 8 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 at 14:45

2 Answers 2

up vote 7 down vote accepted

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;

Your variable inn has already died by that point.

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.

share|improve this answer
    
Thank you Dphil!! –  Ani Apr 8 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'.

share|improve this answer
    
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 at 14:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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