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
TanDTanD 

How to bind data from multiple object in a single visualforce page?

I have two objects Speaker__c, Session__c. I want to create a single VF page which will includes fields from both objects. VF page will have inputField / inputText type of field. The class through error: Initial terms of field expression must be a concrete sObject: List
VF code through the error: Unknown property 'VFpageName.Session__c'
If I dont initiate objects with List<>, apex class will be fine, but VF page shows same error. How to fix this?
My code: 
Class 
public class SingleVFPagePullDataFromMultipleObjects {
    // MVC concept to data binding in VF page & controller
    public List<Speaker__c> objectSpeaker {get; set;}
    public List<Session__c> objectSession {get; set;}
    
    //define the function
    public SingleVFPagePullDataFromMultipleObjects(){
    // Initiate objects 
        List<Speaker__c> objectSpeaker = new List<Speaker__c>();
        List<Session__c> objectSession = new List<Session__c>();
    }
    // Save button
    public void save()
    {
    	try
        {
            insert objectSpeaker;
            objectSession.Speaker_Name__c = objectSpeaker.id;
            insert objectSession;
        }   
        catch(Exception ex)
        {
            System.debug('\n\nException ='+ex.getMessage()+'\n\n');
        }    
    }
    public void cancel(){}    
}

VF
<apex:page controller="SingleVFPagePullDataFromMultipleObjects" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>

                <apex:inputText value="{!Session__c.Session_Date__c }" />
                <apex:inputText value="{!Session__c.Description__c }" />
                <apex:inputText value="{!Session__c.Level__c }" />
                <apex:inputText value="{!Speaker__c.Last_Name__c }" />
                <apex:inputText value="{!Speaker__c.Bio__c }" />                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by TanD
badibadi
You cannot access Session__c directly in your visualforce page as you are using custom controller. You have to use variable name objectSession. Please make sure your visualforce page is updated with this code 
 
<apex:page controller="SingleVFPagePullDataFromMultipleObjects" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>

                <apex:inputText value="{!objectSession.Session_Date__c }" />
                <apex:inputText value="{!objectSession.Description__c }" />
                <apex:inputText value="{!objectSession.Level__c }" />
                <apex:inputText value="{!objectSpeaker.Last_Name__c }" />
                <apex:inputText value="{!objectSpeaker.Bio__c }" />                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

let me know if it works 

 

All Answers

badibadi

try this 
 
public class SingleVFPagePullDataFromMultipleObjects {
    // MVC concept to data binding in VF page & controller
    public Speaker__c objectSpeaker {get; set;}
    public Session__c objectSession {get; set;}
    
    //define the function
    public SingleVFPagePullDataFromMultipleObjects(){
    // Initiate objects 
         objectSpeaker = new Speaker__c();
         objectSession = new Session__c();
    }
    // Save button
    public void save()
    {
    	try
        {
            insert objectSpeaker;
            objectSession.Speaker_Name__c = objectSpeaker.id;
            insert objectSession;
        }   
        catch(Exception ex)
        {
            System.debug('\n\nException ='+ex.getMessage()+'\n\n');
        }    
    }
    public void cancel(){}    
}

and VF
 
<apex:page controller="SingleVFPagePullDataFromMultipleObjects" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>

                <apex:inputText value="{!objectSession.Session_Date__c }" />
                <apex:inputText value="{!objectSession.Description__c }" />
                <apex:inputText value="{!objectSession.Level__c }" />
                <apex:inputText value="{!objectSpeaker.Last_Name__c }" />
                <apex:inputText value="{!objectSpeaker.Bio__c }" />                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
TanDTanD
As I said, without LIST<>, apex code is fine, but same error in VF 
:( 
badibadi
You cannot access Session__c directly in your visualforce page as you are using custom controller. You have to use variable name objectSession. Please make sure your visualforce page is updated with this code 
 
<apex:page controller="SingleVFPagePullDataFromMultipleObjects" >
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>

                <apex:inputText value="{!objectSession.Session_Date__c }" />
                <apex:inputText value="{!objectSession.Description__c }" />
                <apex:inputText value="{!objectSession.Level__c }" />
                <apex:inputText value="{!objectSpeaker.Last_Name__c }" />
                <apex:inputText value="{!objectSpeaker.Bio__c }" />                
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

let me know if it works 

 
This was selected as the best answer
TanDTanD
Thanks. It works. 
Can you please have a look in the below problem:
Show input field with their format & whether it is required
badibadi
Change Visualforce to 
 
<apex:pageBlockSection>
                <apex:inputField value="{!objectSession.Name }" required="true"  /> 
                <apex:inputField value="{!objectSession.Session_Date__c }" />
                <apex:inputField value="{!objectSpeaker.Last_Name__c }" />               
            </apex:pageBlockSection>