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
NavaForceNavaForce 

Unknown Property Error Using Custom Controller in Visualforce Page

I am a brand new developer starting from scratch. I've been working on the following issue for over a month and would greatly appreciate some help.

Data Model: I have a parent object called Financial_Month__c with chid records of Workout__c. This is a master-detail relationship on the Workout__c object.

Desired Endstate: Override New standard button on Workout__c object tab with a custom Visualforce page. On that page, prepopulate the parent (Financial_Month__c) lookup value.

Process:
  1. Created custom controller (see below)
  2. Created Visualforce page that references custom controller.
Errors:
  • Before I enter the workoutMonth variable in the Visualforce page, I receive the following error: "Unknown property 'custom_Controller_New_Workout_VF_Page.Workout__c"
  • After I enter the workoutMonth variable in the pageBlockSection, I receive the following error: "Invalid field Workout__c for SObject Financial_Month__c"
Custom Controller
public class custom_Controller_New_Workout_VF_Page 
{

    //Create a list to store Workout Month records.
    public Financial_Month__c workoutMonth {get; set;}
    
    public custom_Controller_New_Workout_VF_Page()
    {          
    	//Get current Financial Month record.
    	workoutMonth = [SELECT Id,
							   Name,
                               CreatedDate,
                               Current_Month__c,
                               RecordType.DeveloperName
                          FROM Financial_Month__c
                         WHERE RecordType.DeveloperName = 'Workout_Month' AND Current_Month__c = True
                      ORDER By CreatedDate DESC
                         LIMIT 1];
    }
     public Financial_Month__c getworkoutMonth() {
        return workoutMonth;
    }

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

Visualforce Page
<apex:page controller="custom_Controller_New_Workout_VF_Page" lightningStylesheets="true">           
    <apex:form>
        <apex:pageBlock mode="edit">            
        <apex:pageMessages />
            
             <apex:pageBlockSection columns="1"  title="🏋️‍♂️ New Workout" collapsible="false" showHeader="true"> 
                  <apex:outputField value="{!workoutMonth.Workout__c.Workout_Month__c}"/> 
                  <apex:inputField value="{!Workout__c.Date__c}" required="true"/>
                  <apex:inputField value="{!Workout__c.Workout__c}" required="true"/>
                  <apex:inputField value="{!Workout__c.Rest_Reason__c}"/>
             </apex:pageBlockSection>
        
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Save"/>   
        </apex:pageBlockButtons>
 
        </apex:pageBlock>  
    </apex:form>
</apex:page>

I'm getting ready to quit on this project but wanted to see if someone could help me first.

Any thoughts you have would be greatly appreciated. Thank you in advance!
Best Answer chosen by NavaForce
{tushar-sharma}{tushar-sharma}
I am refactoring your code
public workoutExtension(ApexPages.StandardController stdController)
    {
                this.newWorkout = (workout__c)stdController.getRecord();
        
        //Get current Financial Month record.
    	Financial_Month__c workoutMonth = [SELECT Id,
							                      Name,
                                                  CreatedDate,
                                                  Current_Month__c,
                                                  RecordType.DeveloperName
                                             FROM Financial_Month__c
                                            WHERE RecordType.DeveloperName = 'Workout_Month' AND Current_Month__c = True
                                         ORDER By CreatedDate DESC
                                            LIMIT 1];
        
        /* If you are editing existing record then these details will already come but I believe you are creating new record
       //Populate variable for Workout record.
        newWorkout = [SELECT Id,
                             Name,
                             Workout_Month__c
                        FROM Workout__c
                       WHERE Workout_Month__c = :workoutMonth.Id
                       LIMIT 1];*/
        //So do this
       newWorkout.Workout_Month__c = :workoutMonth.Id

    }

Also I suggest you to try Trailhead first.
 

All Answers

{tushar-sharma}{tushar-sharma}
Here you are passing API names in wrong format. It should be like
<apex:outputField value="{!workoutMonth.Workout_Month__c}"/> 
<apex:inputField value="{!workoutMonth.Date__c}" required="true"/>
 <apex:inputField value="{!workoutMonth.Workout__c}" required="true"/>
<apex:inputField value="{!workoutMonth.Rest_Reason__c}"/>

You need to use StandardController and extension to replace New button, while you are using controller so it won't work.
I suggest you to check below trailhead links:
https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals
https://trailhead.salesforce.com/en/content/learn/projects/workshop-override-standard-action/override_4

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
{tushar-sharma}{tushar-sharma}
I didn't notic you have created instance of parent object, As you are trying to create child, so create instance of child.
NavaForceNavaForce
I spent several hours implementing your feedback and am still having difficulty. I changed the following items, per your suggestion: created child instead of parent, used standard controller and created controller extension. The errors went away however, I still cannot populate any of the fields in the Visualforce page with default values, which was the goal of this project. 

Revised Visualforce Page
<apex:page standardController="workout__c" extensions="workoutExtension" lightningStylesheets="true">           
    <apex:form>
        <apex:pageBlock mode="edit">            
        <apex:pageMessages/>
      
             <apex:pageBlockSection columns="1"  title="🏋️‍♂️ New Workout" collapsible="false" showHeader="true"> 
                  <apex:outputField value="{!newWorkout.Workout_Month__c}"/> 
                  <apex:inputField value="{!Workout__c.Date__c}" required="true"/>
                  <apex:inputField value="{!Workout__c.Workout__c}" required="true"/>
                  <apex:inputField value="{!Workout__c.Rest_Reason__c}"/>
             </apex:pageBlockSection>
     
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Save"/>   
        </apex:pageBlockButtons>
 
        </apex:pageBlock>  
    </apex:form>
</apex:page>
Controller Extension Class
public class workoutExtension 
{

    //Create a variable to store Workout record.
    public workout__c newWorkout {get; set;}
    
    public workoutExtension(ApexPages.StandardController stdController)
    {
                this.newWorkout = (workout__c)stdController.getRecord();
    }

    {          
        //Get current Financial Month record.
    	Financial_Month__c workoutMonth = [SELECT Id,
							                      Name,
                                                  CreatedDate,
                                                  Current_Month__c,
                                                  RecordType.DeveloperName
                                             FROM Financial_Month__c
                                            WHERE RecordType.DeveloperName = 'Workout_Month' AND Current_Month__c = True
                                         ORDER By CreatedDate DESC
                                            LIMIT 1];
        
        //Populate variable for Workout record.
        newWorkout = [SELECT Id,
                             Name,
                             Workout_Month__c
                        FROM Workout__c
                       WHERE Workout_Month__c = :workoutMonth.Id
                       LIMIT 1];
    }
}
Can you please tell me what I am still doing wrong? Thank you!
 
{tushar-sharma}{tushar-sharma}
I am refactoring your code
public workoutExtension(ApexPages.StandardController stdController)
    {
                this.newWorkout = (workout__c)stdController.getRecord();
        
        //Get current Financial Month record.
    	Financial_Month__c workoutMonth = [SELECT Id,
							                      Name,
                                                  CreatedDate,
                                                  Current_Month__c,
                                                  RecordType.DeveloperName
                                             FROM Financial_Month__c
                                            WHERE RecordType.DeveloperName = 'Workout_Month' AND Current_Month__c = True
                                         ORDER By CreatedDate DESC
                                            LIMIT 1];
        
        /* If you are editing existing record then these details will already come but I believe you are creating new record
       //Populate variable for Workout record.
        newWorkout = [SELECT Id,
                             Name,
                             Workout_Month__c
                        FROM Workout__c
                       WHERE Workout_Month__c = :workoutMonth.Id
                       LIMIT 1];*/
        //So do this
       newWorkout.Workout_Month__c = :workoutMonth.Id

    }

Also I suggest you to try Trailhead first.
 
This was selected as the best answer
NavaForceNavaForce
Thank you! It worked! I greatly appreciate the help. I didn't realize that I did not need the SOQL query for workout__c and that my curly braces were incorrect and were preventing the code from working correctly. 
I am setting this as best answer.