1
Public class productavailcheck{

    public string PID{get; set;}
    public list<inventory__c> productlist{get; set;}

    public productavailcheck(){

        productlist   =[select Stocks__c,productid__c,name from Inventory__c where productid__c =: PID];

    } 

    public pagereference finalbuybutton(){


        for(inventory__c stcheck : productlist)
            if(PID==stcheck.productid__c) 
            {

                productlist.stocks__c = productlist.stocks__c - 1;

            }
        update productlist;




        return null;
    }





}

PID is passed from another VF page through pagereference. in the if condition when i try to use products__c or stocks__c ,it shows me the error

Error: Compile Error: Variable does not exist: stocks__c at line 19 column 18

and when that if block is empty, no compile errors are there. What is that i am doing wrong?

2 Answers 2

6

Because List does not have any property named Stocks__c. You need to reference the individual record, not the collection.

Collection

for (Inventory__c record : productList)
{
    productList.Stocks__c ...
}

Record

for (Inventory__c record : productList)
{
    record.Stocks__c ...
}

In your case, you need to change productList.Stocks__c to stcheck.Stocks__c.

0
2

Productlist is a list of Inventory__c objects. And your are trying to access stocks__c from the List, whereas you should access it from individual record. Use [] operator to access the record from the list.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .