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
Air FoxxAir Foxx 

Simple class to check invoice amounts

I'm still very new to coding so any help would be appreciated. I'm trying to write a simple class that sums the value of invoice amounts if the invoice numbers are the same. Here's the relevant section that throw this list type error:
"Expression must be a list type: Promo_Invoice__c at line 30 column 34"

List<Promo_Invoice__c> invoices = [SELECT Amount__c, External_ID__c, Invoice_Date__c, Invoice_Number__c, ISBN__c, Quantity__c FROM Promo_Invoice__c WHERE Promo_Sales__c = :promoID AND Invoice_Date__c < :PromoStart ORDER BY Invoice_Number__c DESC];
            for (Promo_Invoice__c eachinvoice : invoices)
            {
                if (eachinvoice[InvIndex].Invoice_Number__c = eachinvoice[InvIndex++].Invoice_Number__c) 
                {
                InvTotalAmount = eachinvoice[InvIndex].Amount__c + eachinvoice[InvIndex++].Amount__c;
                }
Thanks again for any help.
Air FoxxAir Foxx
BTW, line 30 is the last line that begins with "InvTotalAmount."
kaustav goswamikaustav goswami
The way you are iterating over the items in the list in the for loop is wrong.
That variable "eachInvoice" is a single instance - an object. It needs to be referenced with the "."(dot) notation.

Your code tries to access values in that object by index number as if it is an array. If you have to compare and traverse through the list then you have  to use the for loop - the one you used over here is known as the for each loop. It shoul look something like this -

for(int i=0; i<invoices.size(); i++){
    if((i != invoices.size()-1) && (invoices[i].Invoice_Number__c == invoices[i++].Invoice_Number__c)){ // the first clause is to prevent a list out of                                                                                                                                                      //bound exception
        InvTotalAmount = invoices[i].Amount__c + invoices[i++].Amount__c;
    }
}

Hope this gives you an idea and helps you to solve the problem. If you need more help please post more code along with desired functionality.

Thanks,
Kaustav
Air FoxxAir Foxx
Thanks Kaustav. This was helpful.