function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
howard zien 24howard zien 24 

i am trying to loop thu an object 2 ways . the wrong way with a lop and the right way with a list

i am trying to loop thu an object 2 ways . the wrong way with a lop and the right way with a list.
neither one works.

the wrong way, with a loop looks like this.

// First get the new invoice statement
integer i = 0;
for (invoice_statement__c[] inv : [SELECT id FROM Invoice_Statement__c]);
     {
//WHERE Description__c='My new invoice'];
   system.debug(inv.id );
     }

i get this error message.
Line: 7, Column: 14
Variable does not exist: inv.id

i am too confused to write the syntax with a list. and could use some help.

 
Alexander TsitsuraAlexander Tsitsura
hi howard

you need interate thtought list, becouse inv it's invoice_statement__c[] - list.

try this 
// First get the new invoice statement
integer i = 0;
for (invoice_statement__c[] inv : [SELECT id FROM Invoice_Statement__c]);
{
    for (invoice_statement__c i : inv) {
      system.debug(i.id );
    }
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
Shyama B SShyama B S
Hi Howard,
Please give this a try and let me know:
// First get the new invoice statement
integer i = 0;
for (invoice_statement__c inv : [SELECT id FROM Invoice_Statement__c])
     {
          //WHERE Description__c='My new invoice'];
          system.debug(inv.id );
     }

Thanks,
Shyama
Abhishek BansalAbhishek Bansal
Hi Howard,

Please use the below code syntax as per your requirement :
integer i = 0;
for (invoice_statement__c inv : [SELECT id FROM Invoice_Statement__c WHERE Description__c = 'My new invoice']){
	system.debug(inv.id );
}
The query will return records in chunk and assign a single instance of it to the "inv" varibale.
Now you can use this inv varibale to perform action on the record.

Let me know if you need more help on this.

Thanks,
Abhishek