7
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;
    }
}
  • 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 '14 at 14:07
  • 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 '14 at 14:31
  • 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
  • BTW - using a method called display() to do updates is likely to lead to long run maintenance issues – cropredy Apr 8 '14 at 18:53
13

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 should be 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.

| improve this answer | |
7

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'.

| 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 '14 at 14:25

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.