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
Josh Gruberman 12Josh Gruberman 12 

Unknown Property on Custom Controller

I'm brand new to classes and controllers so I'm sure this is an easy fix but I'm getting an error of "Unknown property 'InventoryController.Inventory__c".  I'm just trying to start with some simple code to make sure I know what I'm doing - clearly I don't :)  I started with a Snippet from the VF Dev Guide.  Anyone give me a hand with this?

Apex Class
public class InventoryController {

    private final Inventory__c inventory;

    public InventoryController() {
        Inventory = [SELECT Id, Name, Year__c, Make__c, Model__c, Description__c, Price__c FROM Inventory__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Inventory__c getInventory() {
        return inventory;
    }

    public PageReference save() {
        update inventory;
        return null;
    }
}

Visualforce Page
<apex:page controller="InventoryController" sidebar="false" showHeader="false">
    <apex:form>
        <apex:pageBlock>
            Year: <apex:inputField value="{!Inventory__c.Year__c}"/>
            Make: <apex:inputField value="{!Inventory__c.Make__c}"/>
            Model: <apex:inputField value="{!Inventory__c.Model__c}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Vignaesh Ram AmarnathVignaesh Ram Amarnath
Replace Line 3 of Apex class with the below code
public Inventory__c inventory{get;set;}
Use the variable inventory instead of Inventory__c in visualforce page.
eg: {!inventory.Year__c]
 
Saurabh BSaurabh B
Try this,
 
public class InventoryController {

     public Inventory__c inventory{get;set;}

    public InventoryController() {
        Inventory = [SELECT Id, Name, Year__c, Make__c, Model__c, Description__c, Price__c FROM Inventory__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Inventory__c getInventory() {
        return inventory;
    }

    public PageReference save() {
        update inventory;
        return null;
    }
}
 
<apex:page controller="InventoryController" sidebar="false" showHeader="false">
    <apex:form>
        <apex:pageBlock>
            Year: <apex:inputField value="{!inventory.Year__c}"/>
            Make: <apex:inputField value="{!inventory.Make__c}"/>
            Model: <apex:inputField value="{!inventory.Model__c}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark this as Best Answer if it helps!